ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
autocast.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16
17// =========================================== Module ==========================================
18#if ALIB_C20_MODULES
21 import ALib.Strings;
22#else
24#endif
25// ====================================== Implementation =======================================
26//! @cond NO_DOX
27
28#define ARG0 (*args)
29#define BOL(box) (box).Unbox<bool >()
30#define INT(box) (box).Unbox<integer>()
31//#define FLT(box) (box).Unbox<double >()
32#define FUNC( name,...) Box name( Scope& scope, \
33 ArgIterator args, \
34 ArgIterator end ) \
35 { (void) scope; (void) args; (void) end; __VA_ARGS__ }
36
37
38namespace alib { namespace expressions { namespace plugins {
39
41: CompilerPlugin( "ALib Auto Cast", compiler, CompilePriorities::AutoCast )
42{}
43
44namespace {
45
46enum class SortedTypes
47{
48 UNKNOWN = 0,
49 Bool = 1,
50 Integer = 2,
51 Float = 4,
52 String = 8,
53};
54
55SortedTypes SortedType( Type type )
56{
57 if( type.IsType<bool >() ) return SortedTypes::Bool;
58 if( type.IsType<integer>() ) return SortedTypes::Integer;
59 if( type.IsType<double >() ) return SortedTypes::Float;
60 if( type.IsType<String >() ) return SortedTypes::String;
61 return SortedTypes::UNKNOWN;
62}
63
64// conversion integer to float.
65FUNC( castI2F , return double ( INT(ARG0) ); )
66FUNC( castB2F , return double ( BOL(ARG0) ); )
67FUNC( castB2I , return integer( BOL(ARG0)); )
68
69} // anonymous namespace
70
71
72// #################################################################################################
73// ### AutoCast - TryCompilation
74// #################################################################################################
75bool AutoCast::TryCompilation( CIAutoCast& ciAutoCast )
76{
77 // we do not work on unary ops
78 if ( ciAutoCast.ArgsBegin + 1 >= ciAutoCast.ArgsEnd )
79 return false;
80
81 // convert the minor type to the major
82 SortedTypes t1= SortedType( * ciAutoCast.ArgsBegin );
83 SortedTypes t2= SortedType( *(ciAutoCast.ArgsBegin+1) );
84 if( t1 == t2 )
85 return false;
86 SortedTypes major;
87 SortedTypes minor;
88 if( t1 > t2 ) { major= t1; minor= t2; }
89 else { major= t2; minor= t1; }
90
91 // choose upgrade function
92 CallbackDecl upgradeCast;
93 String decompileFunctionCall;
94 ALIB_DBG( const char* upgradeCastDBG; )
95
96 // we can cast every type to string
97 if( major == SortedTypes::String )
98 {
99 upgradeCast = CBToString;
100 decompileFunctionCall = A_CHAR("String");
101ALIB_DBG(upgradeCastDBG = "CBToString"; )
102 }
103
104 // integer to float?
105 else if( major == SortedTypes::Float && minor == SortedTypes::Integer )
106 {
107 upgradeCast = castI2F;
108 decompileFunctionCall = A_CHAR("Float");
109ALIB_DBG(upgradeCastDBG = "castI2F"; )
110
111 }
112
113 // bool to float?
114 else if( major == SortedTypes::Float && minor == SortedTypes::Bool )
115 {
116 upgradeCast = castB2F;
117 decompileFunctionCall = A_CHAR("Float");
118ALIB_DBG(upgradeCastDBG = "castB2F"; )
119
120 }
121
122 // we can cast every type to integer via boolean
123 else if( major == SortedTypes::Integer && minor == SortedTypes::Bool )
124 {
125 upgradeCast = castB2I;
126 decompileFunctionCall = A_CHAR("Integer");
127ALIB_DBG(upgradeCastDBG = "castB2I"; )
128 }
129
130 // we can cast every type to boolean as well
131 else if( major == SortedTypes::Bool )
132 {
133 upgradeCast = ToBoolean;
134 decompileFunctionCall = A_CHAR("Boolean");
135ALIB_DBG(upgradeCastDBG = "ToBoolean"; )
136 }
137
138 else
139 return false;
140
141 // set upgrade function to the right callback
142 if( t1 < t2 )
143 {
144 ciAutoCast.Callback = upgradeCast;
145 ciAutoCast.TypeOrValue = *(ciAutoCast.ArgsBegin + 1);
146 ciAutoCast.ReverseCastFunctionName = decompileFunctionCall;
147ALIB_DBG(ciAutoCast.DbgCallbackName = upgradeCastDBG; )
148 }
149 else
150 {
151 ciAutoCast.CallbackRhs = upgradeCast;
152 ciAutoCast.TypeOrValueRhs = *(ciAutoCast.ArgsBegin );
153 ciAutoCast.ReverseCastFunctionNameRhs = decompileFunctionCall;
154ALIB_DBG(ciAutoCast.DbgCallbackNameRhs = upgradeCastDBG; )
155 }
156
157 // If constant values were given, we can we do it right away (compile-time optimization)
158 if( ciAutoCast.IsConst && ciAutoCast.Callback != nullptr )
159 {
160 ciAutoCast.TypeOrValue = ciAutoCast.Callback( ciAutoCast.CompileTimeScope,
161 ciAutoCast.ArgsBegin ,
162 ciAutoCast.ArgsEnd - 1 );
163 ciAutoCast.Callback = nullptr;
164 }
165 else if( ciAutoCast.RhsIsConst && ciAutoCast.CallbackRhs != nullptr )
166 {
167 ciAutoCast.TypeOrValueRhs= ciAutoCast.CallbackRhs( ciAutoCast.CompileTimeScope,
168 ciAutoCast.ArgsBegin + 1 ,
169 ciAutoCast.ArgsEnd );
170 ciAutoCast.CallbackRhs = nullptr;
171 }
172
173
174 return true;
175}
176
177
178
179}}} // namespace [alib::expressions::detail]
180
181#undef ARG0
182#undef BOL
183#undef INT
184//#undef FLT
185#undef FUNC
186//! @endcond
#define A_CHAR(STR)
#define ALIB_DBG(...)
Definition alib.inl:836
ALIB_DLL Box ToBoolean(Scope &scope, ArgIterator args, ArgIterator)
ALIB_DLL Box CBToString(Scope &scope, ArgIterator args, ArgIterator)
Box(*)(Scope &scope, ArgIterator argsBegin, ArgIterator argsEnd) CallbackDecl
platform_specific integer
Definition integers.inl:32
expressions::CompilerPlugin CompilerPlugin
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.inl:574
ALIB_DLL AutoCast(Compiler &compiler)