1 /*
2  * Copyright 2019 by its authors. See AUTHORS.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef EOLIAN_MONO_GENERATION_CONTEXTS_HH
17 #define EOLIAN_MONO_GENERATION_CONTEXTS_HH
18 
19 #include <map>
20 
21 #include "grammar/context.hpp"
22 #include "grammar/indentation.hpp"
23 
24 namespace eolian_mono {
25 
26 struct class_context
27 {
28     enum wrapper_kind {
29         interface,
30         concrete,
31         inherit,
32         inherit_native,
33         structs,
34         enums,
35         function_ptr,
36         alias,
37         variables,
38     };
39     wrapper_kind current_wrapper_kind;
40     std::string name;
41 
class_contexteolian_mono::class_context42     class_context(wrapper_kind current_wrapper_kind)
43       :  current_wrapper_kind(current_wrapper_kind)
44       , name()
45     {}
46 
class_contexteolian_mono::class_context47     class_context(wrapper_kind current_wrapper_kind, std::string const& name)
48       :  current_wrapper_kind(current_wrapper_kind)
49       , name(name)
50     {}
51 };
52 
53 struct direction_context
54 {
55     enum direction {
56         native_to_managed,
57         managed_to_native,
58     };
59     direction current_direction;
60 
direction_contexteolian_mono::direction_context61     direction_context(direction current_direction)
62       :  current_direction(current_direction)
63     {}
64 };
65 
66 struct indentation_context
67 {
68   constexpr indentation_context(indentation_context const& other) = default;
indentation_contexteolian_mono::indentation_context69   constexpr indentation_context(efl::eolian::grammar::scope_tab_generator indent)
70     : indent(indent)
71   {}
indentation_contexteolian_mono::indentation_context72   constexpr indentation_context(int n)
73     : indent(n)
74   {}
indentation_contexteolian_mono::indentation_context75   constexpr indentation_context(int n, int m)
76     : indent(n, m)
77   {}
78   efl::eolian::grammar::scope_tab_generator indent;
79 };
80 
81 template <typename Context>
current_indentation(Context const & context)82 inline constexpr efl::eolian::grammar::scope_tab_generator const& current_indentation(Context const& context)
83 {
84   return efl::eolian::grammar::context_find_tag<indentation_context>(context).indent;
85 }
86 
87 template <typename Context>
change_indentation(efl::eolian::grammar::scope_tab_generator const & indent,Context const & context)88 inline constexpr Context change_indentation(efl::eolian::grammar::scope_tab_generator const& indent, Context const& context)
89 {
90   return efl::eolian::grammar::context_replace_tag(indentation_context(indent), context);
91 }
92 
93 struct library_context
94 {
95   std::string library_name;
96   int v_major;
97   int v_minor;
98   std::map<const std::string, std::string> references;
99 
100   const std::string actual_library_name(const std::string& filename) const;
101 };
102 
103 const std::string
actual_library_name(const std::string & filename) const104 library_context::actual_library_name(const std::string& filename) const
105 {
106     // Libraries mapped follow the efl.Libs.NAME scheme.
107     // TODO What about references from outside efl (not present in the efl.Libs class?)
108     auto ref = references.find(filename);
109     if (ref != references.end())
110       return "efl.Libs." + ref->second;
111 
112     // Fallback to original behaviour with explicit library name
113     return '"' + library_name + '"';
114 }
115 
116 struct eolian_state_context {
117     const Eolian_State *state;
118 };
119 
120 struct options_context {
121     bool want_beta;
122     std::string examples_dir;
123 };
124 
125 }
126 
127 #endif
128