summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-02-28 11:09:38 -0300
committerVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-02-28 12:05:36 -0300
commitf78be36dfea490e62943a751dfee6347f3212a97 (patch)
treeba8863150008ec928573c4c3403fc151a373746d
parenta25c0d713754a7fc153ff83c89212489d72ab4c0 (diff)
efl_csharp: add constant variable generation to C# binding (constants)
Reviewers: lauromoura, q66, bu5hm4n, zmike, cedric, felipealmeida, segfaultxavi Reviewed By: segfaultxavi Subscribers: segfaultxavi, cedric, #reviewers, #committers Tags: #efl_language_bindings Differential Revision: https://phab.enlightenment.org/D8048
-rw-r--r--src/Makefile_Efl_Mono.am1
-rw-r--r--src/bin/eolian_mono/eolian/mono/generation_contexts.hh1
-rw-r--r--src/bin/eolian_mono/eolian/mono/variable_definition.hh93
-rw-r--r--src/bin/eolian_mono/eolian_mono.cc15
-rw-r--r--src/lib/eolian_cxx/grammar/klass_def.hpp64
-rw-r--r--src/tests/efl_mono/Eo.cs18
-rw-r--r--src/tests/efl_mono/dummy_test_object.eo12
7 files changed, 204 insertions, 0 deletions
diff --git a/src/Makefile_Efl_Mono.am b/src/Makefile_Efl_Mono.am
index 1619411..fe8c66e 100644
--- a/src/Makefile_Efl_Mono.am
+++ b/src/Makefile_Efl_Mono.am
@@ -82,6 +82,7 @@ bin_eolian_mono_eolian_mono_SOURCES = \
82 bin/eolian_mono/eolian/mono/part_definition.hh \ 82 bin/eolian_mono/eolian/mono/part_definition.hh \
83 bin/eolian_mono/eolian/mono/struct_fields.hh \ 83 bin/eolian_mono/eolian/mono/struct_fields.hh \
84 bin/eolian_mono/eolian/mono/parameter.hh \ 84 bin/eolian_mono/eolian/mono/parameter.hh \
85 bin/eolian_mono/eolian/mono/variable_definition.hh \
85 bin/eolian_mono/eolian/mono/utils.hh \ 86 bin/eolian_mono/eolian/mono/utils.hh \
86 bin/eolian_mono/eolian/mono/using_decl.hh \ 87 bin/eolian_mono/eolian/mono/using_decl.hh \
87 bin/eolian_mono/eolian/mono/marshall_type.hh \ 88 bin/eolian_mono/eolian/mono/marshall_type.hh \
diff --git a/src/bin/eolian_mono/eolian/mono/generation_contexts.hh b/src/bin/eolian_mono/eolian/mono/generation_contexts.hh
index 9a1493a..5cc57ef 100644
--- a/src/bin/eolian_mono/eolian/mono/generation_contexts.hh
+++ b/src/bin/eolian_mono/eolian/mono/generation_contexts.hh
@@ -14,6 +14,7 @@ struct class_context
14 enums, 14 enums,
15 function_ptr, 15 function_ptr,
16 alias, 16 alias,
17 variables,
17 }; 18 };
18 wrapper_kind current_wrapper_kind; 19 wrapper_kind current_wrapper_kind;
19}; 20};
diff --git a/src/bin/eolian_mono/eolian/mono/variable_definition.hh b/src/bin/eolian_mono/eolian/mono/variable_definition.hh
new file mode 100644
index 0000000..2a0eac3
--- /dev/null
+++ b/src/bin/eolian_mono/eolian/mono/variable_definition.hh
@@ -0,0 +1,93 @@
1#ifndef EOLIAN_MONO_VARIABLE_DEFINITION_HH
2#define EOLIAN_MONO_VARIABLE_DEFINITION_HH
3
4#include <Eina.hh>
5
6#include "grammar/generator.hpp"
7#include "grammar/klass_def.hpp"
8
9#include "grammar/indentation.hpp"
10#include "grammar/list.hpp"
11#include "grammar/alternative.hpp"
12#include "grammar/attribute_reorder.hpp"
13#include "logging.hh"
14#include "type.hh"
15#include "name_helpers.hh"
16#include "helpers.hh"
17#include "function_helpers.hh"
18#include "marshall_type.hh"
19#include "parameter.hh"
20#include "documentation.hh"
21#include "using_decl.hh"
22#include "generation_contexts.hh"
23#include "blacklist.hh"
24
25namespace eolian_mono {
26
27
28struct constant_definition_generator
29{
30 template<typename OutputIterator, typename Context>
31 bool generate(OutputIterator sink, attributes::variable_def constant, Context context) const
32 {
33 // Open partial class
34 if (!name_helpers::open_namespaces(sink, constant.namespaces, context))
35 return false;
36
37 if (!as_generator("public partial class Constants {\n").generate(sink, attributes::unused, context))
38 return false;
39
40 std::string literal;
41 if (constant.expression_value.type == EOLIAN_EXPR_NULL)
42 literal = "null";
43 else if (constant.expression_value.type == EOLIAN_EXPR_BOOL)
44 literal = (constant.expression_value.value.b ? "true" : "false");
45 else
46 {
47 auto lit = ::eolian_expression_value_to_literal(&constant.expression_value);
48 if (!lit)
49 return false;
50 literal = lit;
51 ::eina_stringshare_del(lit);
52 }
53
54 // declare variable
55 if (!as_generator(scope_tab(1)
56 << "public static readonly " << type
57 << " " << utils::remove_all(constant.name, '_')
58 << " = " << literal << ";\n")
59 .generate(sink, constant.base_type, context))
60 return false;
61
62 // FIXME missing documentation generator
63
64 // Close partial class
65 if (!as_generator("}\n").generate(sink, attributes::unused, context))
66 return false;
67
68 if (!name_helpers::close_namespaces(sink, constant.namespaces, context))
69 return false;
70
71 return true;
72
73
74 }
75} const constant_definition;
76
77}
78
79namespace efl { namespace eolian { namespace grammar {
80
81template <>
82struct is_eager_generator< ::eolian_mono::constant_definition_generator> : std::true_type {};
83template <>
84struct is_generator< ::eolian_mono::constant_definition_generator> : std::true_type {};
85
86namespace type_traits {
87template <>
88struct attributes_needed< ::eolian_mono::constant_definition_generator> : std::integral_constant<int, 1> {};
89}
90
91} } }
92
93#endif
diff --git a/src/bin/eolian_mono/eolian_mono.cc b/src/bin/eolian_mono/eolian_mono.cc
index 235f250..58c725c 100644
--- a/src/bin/eolian_mono/eolian_mono.cc
+++ b/src/bin/eolian_mono/eolian_mono.cc
@@ -33,6 +33,7 @@
33#include <eolian/mono/marshall_annotation.hh> 33#include <eolian/mono/marshall_annotation.hh>
34#include <eolian/mono/function_pointer.hh> 34#include <eolian/mono/function_pointer.hh>
35#include <eolian/mono/alias_definition.hh> 35#include <eolian/mono/alias_definition.hh>
36#include <eolian/mono/variable_definition.hh>
36 37
37namespace eolian_mono { 38namespace eolian_mono {
38 39
@@ -168,6 +169,20 @@ run(options_type const& opts)
168 } 169 }
169 } 170 }
170 171
172 // Constants
173 {
174 auto var_cxt = context_add_tag(class_context{class_context::variables}, context);
175 for (efl::eina::iterator<const Eolian_Variable> var_iterator( ::eolian_state_constants_by_file_get(opts.state, basename_input.c_str()))
176 , var_last; var_iterator != var_last; ++var_iterator)
177 {
178 efl::eolian::grammar::attributes::variable_def var(&*var_iterator, opts.unit);
179 if (!eolian_mono::constant_definition.generate(iterator, var, var_cxt))
180 {
181 throw std::runtime_error("Failed to generate enum");
182 }
183 }
184 }
185
171 if (klass) 186 if (klass)
172 { 187 {
173 efl::eolian::grammar::attributes::klass_def klass_def(klass, opts.unit); 188 efl::eolian::grammar::attributes::klass_def klass_def(klass, opts.unit);
diff --git a/src/lib/eolian_cxx/grammar/klass_def.hpp b/src/lib/eolian_cxx/grammar/klass_def.hpp
index 5fb0dbb..cba2fb1 100644
--- a/src/lib/eolian_cxx/grammar/klass_def.hpp
+++ b/src/lib/eolian_cxx/grammar/klass_def.hpp
@@ -91,6 +91,14 @@ inline typedecl_type typedecl_type_get(Eolian_Typedecl const* decl)
91 } 91 }
92} 92}
93 93
94enum class variable_type
95{
96 unknown,
97 constant,
98 global
99};
100
101
94struct type_def; 102struct type_def;
95bool operator==(type_def const& rhs, type_def const& lhs); 103bool operator==(type_def const& rhs, type_def const& lhs);
96bool operator!=(type_def const& rhs, type_def const& lhs); 104bool operator!=(type_def const& rhs, type_def const& lhs);
@@ -857,6 +865,62 @@ struct property_def
857 } 865 }
858}; 866};
859 867
868struct variable_def
869{
870 std::string name;
871 std::string full_name;
872 type_def base_type;
873 documentation_def documentation;
874 variable_type type;
875 std::vector<std::string> namespaces;
876 Eolian_Value expression_value;
877 bool is_extern : 1;
878
879 friend inline bool operator==(variable_def const& lhs, variable_def const& rhs)
880 {
881 return lhs.name == rhs.name
882 && lhs.full_name == rhs.full_name
883 && lhs.base_type == rhs.base_type
884 && lhs.documentation == rhs.documentation
885 && lhs.type == rhs.type
886 && lhs.namespaces == rhs.namespaces
887 && lhs.expression_value.type == rhs.expression_value.type
888 && lhs.expression_value.value.ll == rhs.expression_value.value.ll
889 && lhs.is_extern == rhs.is_extern;
890 }
891
892 friend inline bool operator!=(variable_def const& lhs, variable_def const& rhs)
893 {
894 return !(lhs == rhs);
895 }
896
897 variable_def() = default;
898 variable_def(Eolian_Variable const* variable, Eolian_Unit const* unit)
899 : name(::eolian_variable_short_name_get(variable))
900 , full_name(::eolian_variable_name_get(variable))
901 , base_type(::eolian_variable_base_type_get(variable), unit, ::EOLIAN_C_TYPE_DEFAULT)
902 , documentation(::eolian_variable_documentation_get(variable))
903 , type(static_cast<variable_type>(::eolian_variable_type_get(variable)))
904 , expression_value()
905 , is_extern(::eolian_variable_is_extern(variable))
906 {
907 for(efl::eina::iterator<const char> namespace_iterator( ::eolian_variable_namespaces_get(variable))
908 , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
909 {
910 this->namespaces.push_back((&*namespace_iterator));
911 }
912
913 if (this->type == variable_type::constant)
914 {
915 auto expr = ::eolian_variable_value_get(variable);
916 if (!expr)
917 throw std::runtime_error("Could not get constant variable value expression");
918
919 this->expression_value = ::eolian_expression_eval_type(expr, ::eolian_variable_base_type_get(variable));
920 }
921 }
922};
923
860 924
861// template <int N> 925// template <int N>
862// struct tuple_element<N, function_def const> : tuple_element<N, function_def> {}; 926// struct tuple_element<N, function_def const> : tuple_element<N, function_def> {};
diff --git a/src/tests/efl_mono/Eo.cs b/src/tests/efl_mono/Eo.cs
index ed1d65a..3e7f662 100644
--- a/src/tests/efl_mono/Eo.cs
+++ b/src/tests/efl_mono/Eo.cs
@@ -190,6 +190,24 @@ class TestTypedefs
190 } 190 }
191} 191}
192 192
193class TestVariables
194{
195 public static void test_constant_variables()
196 {
197 Test.AssertEquals(Dummy.Constants.ConstvarBool, true);
198 Test.AssertEquals(Dummy.Constants.ConstvarInt, -32766);
199 Test.AssertEquals(Dummy.Constants.ConstvarUInt, 65533U);
200 Test.AssertEquals(Dummy.Constants.ConstvarLong, -2147483644L);
201 Test.AssertEquals(Dummy.Constants.ConstvarULong, 4294967288UL);
202 //Test.AssertEquals(Dummy.Constants.ConstvarLLong, -9223372036854775800); // TODO
203 //Test.AssertEquals(Dummy.Constants.ConstvarULLong, 18446744073709551615);
204 Test.AssertEquals(Dummy.Constants.ConstvarFloat, 16777211.0f);
205 Test.AssertEquals(Dummy.Constants.ConstvarDouble, 9007199254740988.0);
206 Test.AssertEquals(Dummy.Constants.ConstvarChar, '!');
207 Test.AssertEquals(Dummy.Constants.ConstvarString, "test_str");
208 }
209}
210
193class TestEoAccessors 211class TestEoAccessors
194{ 212{
195 public static void basic_eo_accessors() 213 public static void basic_eo_accessors()
diff --git a/src/tests/efl_mono/dummy_test_object.eo b/src/tests/efl_mono/dummy_test_object.eo
index 99e8d02..f405cd4 100644
--- a/src/tests/efl_mono/dummy_test_object.eo
+++ b/src/tests/efl_mono/dummy_test_object.eo
@@ -2,6 +2,18 @@ import eina_types;
2 2
3type Dummy.MyInt: int; 3type Dummy.MyInt: int;
4 4
5const Dummy.Constvar_Bool : bool = true;
6const Dummy.Constvar_Int : int = -32766;
7const Dummy.Constvar_UInt : uint = 65533U;
8const Dummy.Constvar_Long : long = -2147483644L;
9const Dummy.Constvar_ULong : ulong = 4294967288UL;
10const Dummy.Constvar_LLong : llong = -9223372036854775800LL;
11const Dummy.Constvar_ULLong : ullong = 18446744073709551615ULL;
12const Dummy.Constvar_Float : float = 16777211.0f;
13const Dummy.Constvar_Double : double = 9007199254740988.0;
14const Dummy.Constvar_Char : char = '!';
15const Dummy.Constvar_String : string = "test_str";
16
5enum Dummy.SampleEnum { 17enum Dummy.SampleEnum {
6 v0, 18 v0,
7 v1, 19 v1,