-
Notifications
You must be signed in to change notification settings - Fork 1
/
TccWrapper.hpp
735 lines (599 loc) · 32 KB
/
TccWrapper.hpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
Convenient header-only C++17 wrapper to use with embedded Tiny C Compiler (tcc).
Define TW_USE_EXCEPTIONS to use with_state/invoke methods
Define TW_USE_OPTIONAL to use opt_with_state/opt_invoke methods
Created by Patrick Stritch
*/
#pragma once
#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L
#define TW_VERSION_MAJOR 1
#define TW_VERSION_MINOR 0
#define TW_VERSION_PATCH 0
// C++
#include <cstdint>
#include <cstring>
#include <utility>
#if defined(TW_USE_EXCEPTIONS)
#include <stdexcept>
#include <string>
#endif
#if defined(TW_USE_OPTIONAL)
#include <optional>
#endif
// tinycc
#include <libtcc.h>
namespace tw
{
namespace priv
{
namespace traits
{
/// Trait to recognize if F is a function
template <typename F>
inline constexpr bool Function_v = std::is_function_v<F>;
/// Trait to recognize if FP is a function pointer
template <typename FP>
inline constexpr bool FunctionPtr_v = std::is_function_v<std::remove_pointer_t<FP>>;
/// Trait to recognize if MP is a method (member function) pointer
template <typename MP>
inline constexpr bool MethodPtr_v = std::is_member_function_pointer_v<MP>;
/// Trait to recognize if F is invokable with Args...
template <typename F, typename... Args>
inline constexpr bool InvokableWith_v = std::is_invocable_v<F, Args...>;
/// Trait to recognize if From is bit-castable to To
template <typename To, typename From>
inline constexpr bool BitCastable_v = (sizeof(To) == sizeof(From)) &&
std::is_trivially_copyable_v<From> &&
std::is_trivial_v<To>;
}
/// Fallback helper for more readable template error messages
template <typename...>
inline constexpr bool error = false;
/// Type-safe reinterpret_cast equivalent that follows type aliasing rules
template <typename To, typename From>
To bit_cast(From const& src) noexcept
{
if constexpr (traits::BitCastable_v<To, From>)
{
To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
}
else
{
static_assert(error<To, From>, "Type requirements to perform bit cast are not met!");
}
}
/// Helper struct holder for as_free_function method
template <auto vMethodPtr, bool vIsNoexcept, bool vIsCVariadic, typename Class, typename Ret, typename... Args>
struct MethodConverterBase
{
/// Convert method pointer to C-like function pointer
static constexpr auto as_free_function() noexcept
{
if constexpr (vIsCVariadic)
{
static_assert(error<Class>, "C-like variadic arguments in method are not supported!");
}
else
{
return +[](std::remove_reference_t<Class>* ptr, Args... args) noexcept(vIsNoexcept) -> Ret {
return (std::forward<Class>(*ptr).*vMethodPtr)(std::forward<Args>(args)...);
};
}
}
};
/// Basic template struct for MethodConverter
template <typename, auto>
struct MethodConverter;
/// MethodConverter specialization for `Ret(Class::*)(Args...)` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...), vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...)` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...), vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile &` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile &, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, false, Class const volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile &&` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile &&, vMethodPtr>
: MethodConverterBase<vMethodPtr, false, true, Class const volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const volatile, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile & noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile & noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const volatile&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) volatile && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) volatile && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args...) const volatile && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args...) const volatile && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, false, Class const volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) volatile && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) volatile && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class volatile&&, Ret, Args...> {};
/// MethodConverter specialization for `Ret(Class::*)(Args..., ...) const volatile && noexcept` signature
template <typename Class, typename Ret, typename... Args, auto vMethodPtr>
struct MethodConverter<Ret(Class::*)(Args..., ...) const volatile && noexcept, vMethodPtr>
: MethodConverterBase<vMethodPtr, true, true, Class const volatile&&, Ret, Args...> {};
/// Convert method pointer to C-like function pointer
template <typename MP, MP vMethodPtr>
constexpr auto as_free_function() noexcept
{
return MethodConverter<MP, vMethodPtr>::as_free_function();
}
/// Convert method pointer to C-like function pointer
template <auto vMethodPtr>
constexpr auto as_free_function() noexcept
{
return as_free_function<decltype(vMethodPtr), vMethodPtr>();
}
}
/// Enumeration that specifies how compiled code will be outputed
enum class OutputType : int32_t
{
Dll = TCC_OUTPUT_DLL,
Executable = TCC_OUTPUT_EXE,
Object = TCC_OUTPUT_OBJ
};
/// Wrapper around tcc state with set of useful methods
class TccWrapper
{
public:
using State_t = TCCState*;
using ErrorFn_t = void (*)(void* user_data, char const* msg);
/// Create wrapper object from (possibly) existing state
static TccWrapper from(State_t state)
{
return TccWrapper{ state };
}
/// Deleted named-ctor to prevent construction from nullptr
static TccWrapper from(std::nullptr_t) = delete;
#if defined(TW_USE_EXCEPTIONS)
/// Create wrapper object with valid state or throw on failure
static TccWrapper with_state()
{
if (auto state = tcc_new())
{
return TccWrapper{ state };
}
throw std::runtime_error("TccWrapper::with_state() - unable to create tcc state");
}
#endif
#if defined(TW_USE_OPTIONAL)
/// Create optional of wrapper object with valid state or empty optional on failure
static std::optional<TccWrapper> opt_with_state()
{
if (auto state = tcc_new())
{
return TccWrapper{ state };
}
return std::nullopt;
}
#endif
/// Create invalid (without state) wrapper object
TccWrapper() noexcept
: m_state { nullptr }
{}
/// Deleted const copy-ctor
TccWrapper(TccWrapper const&) = delete;
/// Deleted const copy-assign-op
TccWrapper& operator=(TccWrapper const&) = delete;
/// Move-ctor
TccWrapper(TccWrapper&& other) noexcept
: m_state { std::exchange(other.m_state, nullptr) }
{}
/// Move-assign-op
TccWrapper& operator=(TccWrapper&& other) noexcept
{
if (this != &other)
{
if (is_valid())
{
tcc_delete(m_state);
}
m_state = std::exchange(other.m_state, nullptr);
}
return *this;
}
/// Destroy tcc state
~TccWrapper() noexcept
{
if (is_valid())
{
tcc_delete(m_state);
}
}
/// Create (or recreate) tcc state, return true on success
bool create_state() noexcept
{
if (is_valid())
{
tcc_delete(m_state);
}
m_state = tcc_new();
return is_valid();
}
/// Destroy tcc state, return true if it was deleted
void destroy_state() noexcept
{
if (is_valid())
{
tcc_delete(m_state);
m_state = nullptr;
}
}
/// Set function for printing error messages
void set_error_callback(void* user_data, ErrorFn_t fn) const noexcept
{
tcc_set_error_func(m_state, user_data, fn);
}
/// Set options as from command line (like "-std=c99 -O2")
void set_options(char const* options) const noexcept
{
tcc_set_options(m_state, options);
}
/// Add include path (as with -Ipath)
void add_include_path(char const* path) const noexcept
{
tcc_add_include_path(m_state, path);
}
/// Add system include path (as with -isystem path)
void add_system_include_path(char const* path) const noexcept
{
tcc_add_sysinclude_path(m_state, path);
}
/// Add library path (as with -Lpath)
void add_library_path(char const* path) const noexcept
{
tcc_add_library_path(m_state, path);
}
/// Add library (as with -lname)
void add_library(char const* name) const noexcept
{
tcc_add_library(m_state, name);
}
/// Add file { C file, dll, object, library, ld script } for compilation, return true on success
bool add_file(char const* path) const noexcept
{
return tcc_add_file(m_state, path) != -1;
}
/// Add null-terminated string containing C source for compilation, return true on success
bool add_source_code(char const* src) const noexcept
{
return tcc_compile_string(m_state, src) != -1;
}
/// Compile code to auto managed memory, return true on successful allocation, call only once
bool compile() const noexcept
{
tcc_set_output_type(m_state, TCC_OUTPUT_MEMORY);
return tcc_relocate(m_state, TCC_RELOCATE_AUTO) != -1;
}
/// Define macro with given name and optional value (as with #define name value)
void define(char const* name, char const* value = nullptr) const noexcept
{
tcc_define_symbol(m_state, name, value);
}
/// Undefine macro with given name (as with #undef name)
void undefine(char const* name) const noexcept
{
tcc_undefine_symbol(m_state, name);
}
/// Add symbol with given name
void add_symbol(char const* name, void const* symbol) const noexcept
{
tcc_add_symbol(m_state, name, symbol);
}
/// Return void pointer to symbol with given name or nullptr if no such symbol exists
void* get_symbol(char const* name) const noexcept
{
return tcc_get_symbol(m_state, name);
}
/// Return T pointer to symbol with given name or nullptr if no such symbol exists
template <typename T>
T* get_symbol_as(char const* name) const noexcept
{
return priv::bit_cast<T*>(get_symbol(name));
}
/// Check if symbol with given name exists
bool has_symbol(char const* name) const noexcept
{
return get_symbol(name) != nullptr;
}
/// Register symbol from parameter as free function with given name, won't override if called multiple times
template <typename FP>
void register_function(char const* name, FP fn) const noexcept
{
if constexpr (priv::traits::FunctionPtr_v<FP>)
{
tcc_add_symbol(m_state, name, priv::bit_cast<void const*>(fn));
}
else
{
static_assert(priv::error<FP>, "FP is not a function pointer!");
}
}
/// Register symbol as free function with given name, won't override if called multiple times
template <typename F, F vFunctionPtr>
void register_function(char const* name) const noexcept
{
register_function(name, vFunctionPtr);
}
/// Register symbol as free function with given name, won't override if called multiple times
template <auto vFunctionPtr>
void register_function(char const* name) const noexcept
{
register_function(name, vFunctionPtr);
}
/// Register symbol as class method with given name, won't override if called multiple times
template <typename MP, MP vMethodPtr>
void register_method(char const* name) const noexcept
{
if constexpr (priv::traits::MethodPtr_v<MP>)
{
register_function(name, priv::as_free_function<MP, vMethodPtr>());
}
else
{
static_assert(priv::error<MP>, "MP is not a method pointer!");
}
}
/// Register symbol as class method with given name, won't override if called multiple times
template <auto vMethodPtr>
void register_method(char const* name) const noexcept
{
register_method<decltype(vMethodPtr), vMethodPtr>(name);
}
/// Return F pointer to function with given name or nullptr if no such symbol exists
template <typename F>
auto get_function(char const* name) const noexcept
{
if constexpr (priv::traits::Function_v<F>)
{
return get_symbol_as<F>(name);
}
else
{
static_assert(priv::error<F>, "F is not a function!");
}
}
#if defined(TW_USE_EXCEPTIONS)
/// Try to invoke function with given args, return result, throw if no such function symbol exists
template <typename F, typename... Args>
auto invoke(char const* name, Args&&... args) const
{
if constexpr (priv::traits::Function_v<F>)
{
if constexpr (priv::traits::InvokableWith_v<F, Args...>)
{
auto const symbol = get_function<F>(name);
if (symbol != nullptr)
{
return (*symbol)(std::forward<Args>(args)...);
}
throw std::runtime_error(std::string{ "TccWrapper::invoke() - unable to find symbol with given name: " } + name);
}
else
{
static_assert(priv::error<F, Args...>, "F is not invokable with given Args!");
}
}
else
{
static_assert(priv::error<F>, "F is not a function!");
}
}
#endif
#if defined(TW_USE_OPTIONAL)
/// Try to invoke function with given args, return optional with call result (with value or empty)
template <typename F, typename... Args>
auto opt_invoke(char const* name, Args&&... args) const
{
if constexpr (priv::traits::Function_v<F>)
{
if constexpr (priv::traits::InvokableWith_v<F, Args...>)
{
auto const symbol = get_function<F>(name);
return symbol != nullptr ? std::make_optional((*symbol)(std::forward<Args>(args)...)) : std::nullopt;
}
else
{
static_assert(priv::error<F, Args...>, "F is not invokable with given Args!");
}
}
else
{
static_assert(priv::error<F>, "F is not a function!");
}
}
#endif
/// Output file depending on output_type, return true on success
bool output_file(char const* filename, OutputType output_type) const noexcept
{
tcc_set_output_type(m_state, static_cast<int32_t>(output_type));
return tcc_output_file(m_state, filename) != -1;
}
/// Return true if internal state is valid (not nullptr)
bool is_valid() const noexcept
{
return m_state != nullptr;
}
/// Return internal state on which wrapper operates
State_t get_state() const noexcept
{
return m_state;
}
private:
/// PRIV: Internal private ctor
TccWrapper(State_t state) noexcept
: m_state { state }
{
}
State_t m_state;
};
}
#else
#error TccWrapper requires at least C++17 capable compiler to work
#endif