summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/eo_class_functions_generator.hh
blob: f99254c3220b921f088a4327b43bc6f7613f7f84 (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

#ifndef EOLIAN_CXX_STD_EO_CLASS_FUNCTIONS_GENERATOR_HH
#define EOLIAN_CXX_STD_EO_CLASS_FUNCTIONS_GENERATOR_HH

#include <iosfwd>

#include "eo_types.hh"
#include "tab.hh"
#include "comment.hh"
#include "parameters_generator.hh"
#include "type_generator.hh"
#include "namespace_generator.hh"
#include "eo_class_scope_guard_generator.hh"

namespace efl { namespace eolian { namespace grammar {

struct function_call
{
   eo_function const& _func;
   function_call(eo_function const& func) : _func(func) {}
};

struct parameterized_obj_function_call
{
   eo_function const& _func;
   std::string obj;
   parameterized_obj_function_call(eo_function const& func, std::string obj) : _func(func), obj(obj) {}
};
      
inline std::ostream&
operator<<(std::ostream& out, function_call const& x)
{
   bool is_void = function_is_void(x._func);
   bool is_static = function_is_static(x._func);
   return out << (!is_void ? "_tmp_ret = " : "")
              << "::" << x._func.impl
              << "("
              << (is_static ? "const_cast<Eo*>(_eo_class())" : "_concrete_eo_ptr()")
              << (x._func.params.empty() ? "" : ",")
              << parameters_forward_to_c(x._func.params) << ")";
}

inline std::ostream&
operator<<(std::ostream& out, parameterized_obj_function_call const& x)
{
   bool is_void = function_is_void(x._func);
   return out << (!is_void ? "_tmp_ret = " : "")
              << "::" << x._func.impl
              << "("
              << x.obj
              << (x._func.params.empty() ? "" : ",")
              << parameters_forward_to_c(x._func.params) << ")";
}
      
struct function_declaration
{
   eo_class const& _cls;
   eo_function const& _func;
   function_declaration(eo_class const& cls, eo_function const& func)
     : _cls(cls), _func(func)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, function_declaration const& x)
{
   eo_function const& func = x._func;

   out << comment(x._func.comment, 1)
       << template_parameters_declaration(func.params, 1)
       << tab(1);

   bool is_static = function_is_static(func);
   if (is_static)
     out << "static ";

   out << reinterpret_type(func.ret) << " " << func.name << "("
       << parameters_declaration(func.params)
       << (is_static ? ");" : ") const;") << endl;

   return out;
}

struct function_definition
{
   eo_class const& _cls;
   eo_function const& _func;
   bool _concrete;
   function_definition(eo_class const& cls, eo_function const& func, bool concrete)
     : _cls(cls), _func(func), _concrete(concrete)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, function_definition const& x)
{
   eo_function const& func = x._func;

   bool is_static = function_is_static(func);

   out << template_parameters_declaration(func.params, 0)
       << "inline " << reinterpret_type(func.ret) << " ";

   if (x._concrete)
     out << full_name(x._cls, false);
   else
     out << abstract_full_name(x._cls, false);

   out << "::" << func.name << "("
       << parameters_declaration(func.params)
       << (is_static ? ")" : ") const") << endl
       << "{" << endl;

   if (!function_is_void(func))
     out << tab(1)
         << func.ret.front().native << " _tmp_ret;" << endl;

   out << callbacks_heap_alloc("_concrete_eo_ptr()", func.params, is_static, 1);

   out << tab(1) << function_call(x._func) << ";" << endl;

   if (!function_is_void(func))
     out << tab(1) << "return " << to_cxx(func.ret, "_tmp_ret") << ";" << endl;

   out << "}" << endl;
   return out;
}

struct function_declarations
{
   eo_class const& _cls;
   function_declarations(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, function_declarations const& x)
{
   for (eo_function const& f : x._cls.functions)
     {
        out << scope_guard_head(x._cls, f)
            << function_declaration(x._cls, f)
            << scope_guard_tail(x._cls, f) << endl;
     }
   return out;
}

struct function_definitions
{
   eo_class const& _cls;
   bool _concrete;
   function_definitions(eo_class const& cls, bool concrete)
     : _cls(cls)
     , _concrete(concrete)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, function_definitions const& x)
{
   for (eo_function const& f : x._cls.functions)
     {
        out << scope_guard_head(x._cls, f)
            << function_definition(x._cls, f, x._concrete)
            << scope_guard_tail(x._cls, f) << endl;
     }
   return out;
}

} } } // namespace efl { namespace eolian { namespace grammar {

#endif // EOLIAN_CXX_STD_EO_CLASS_FUNCTIONS_GENERATOR_HH