GCC Code Coverage Report


Directory: src/
File: src/mir/types.cpp
Date: 2025-10-15 09:43:47
Exec Total Coverage
Lines: 63 64 98.4%
Functions: 9 9 100.0%
Branches: 9 10 90.0%

Line Branch Exec Source
1 #include <gyoji-mir/types.hpp>
2 #include <variant>
3 #include <stdio.h>
4
5 using namespace Gyoji::context;
6 using namespace Gyoji::mir;
7
8 ////////////////////////////////////////
9 // Types
10 ////////////////////////////////////////
11
12 const static std::string zero_source_filename("builtin");
13 static SourceReference zero_source_ref(zero_source_filename, 0, 0, 0);
14
15 32 Types::Types()
16 {
17 // XXX Definitely not the place to do this, but
18 // it's a good enough place for now that I don't care
19 // until we have a type system we can plug into this
20
21 32 define_type(std::make_unique<Type>("i8", Type::TYPE_PRIMITIVE_i8, true, zero_source_ref));
22 32 define_type(std::make_unique<Type>("i16", Type::TYPE_PRIMITIVE_i16, true, zero_source_ref));
23 32 define_type(std::make_unique<Type>("i32", Type::TYPE_PRIMITIVE_i32, true, zero_source_ref));
24 32 define_type(std::make_unique<Type>("i64", Type::TYPE_PRIMITIVE_i64, true, zero_source_ref));
25
26 32 define_type(std::make_unique<Type>("u8", Type::TYPE_PRIMITIVE_u8, true, zero_source_ref));
27 32 define_type(std::make_unique<Type>("u16", Type::TYPE_PRIMITIVE_u16, true, zero_source_ref));
28 32 define_type(std::make_unique<Type>("u32", Type::TYPE_PRIMITIVE_u32, true, zero_source_ref));
29 32 define_type(std::make_unique<Type>("u64", Type::TYPE_PRIMITIVE_u64, true, zero_source_ref));
30
31 32 define_type(std::make_unique<Type>("f32", Type::TYPE_PRIMITIVE_f32, true, zero_source_ref));
32 32 define_type(std::make_unique<Type>("f64", Type::TYPE_PRIMITIVE_f64, true, zero_source_ref));
33
34 32 define_type(std::make_unique<Type>("bool", Type::TYPE_PRIMITIVE_bool, true, zero_source_ref));
35 32 define_type(std::make_unique<Type>("void", Type::TYPE_PRIMITIVE_void, true, zero_source_ref));
36 32 }
37
38 32 Types::~Types()
39 32 {}
40
41 Type *
42 3070 Types::get_type(std::string type) const
43 {
44 3070 const auto & it = type_map.find(type);
45
2/2
✓ Branch 2 taken 460 times.
✓ Branch 3 taken 2610 times.
3070 if (it == type_map.end()) {
46 460 return nullptr;
47 }
48 2610 return it->second.get();
49 }
50 const Type *
51 152 Types::get_pointer_to(const Type *_type, const SourceReference & src_ref)
52 {
53 152 std::string pointer_type_name = _type->get_name() + std::string("*");
54 152 const Type* pointer_type = get_type(pointer_type_name);
55
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 70 times.
152 if (pointer_type != nullptr) {
56 82 return pointer_type;
57 }
58 70 Gyoji::owned<Type> pointer_owned = std::make_unique<Type>(pointer_type_name, Type::TYPE_POINTER, false, src_ref);
59 70 pointer_type = pointer_owned.get();
60 70 pointer_owned->complete_pointer_definition(_type, src_ref);
61 70 define_type(std::move(pointer_owned));
62 70 return pointer_type;
63 152 }
64
65 const Type *
66 8 Types::get_reference_to(const Type *_type, const SourceReference & src_ref)
67 {
68 8 std::string pointer_type_name = _type->get_name() + std::string("&");
69
70 8 const Type* pointer_type = get_type(pointer_type_name);
71
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (pointer_type != nullptr) {
72 2 return pointer_type;
73 }
74 6 Gyoji::owned<Type> pointer_owned = std::make_unique<Type>(pointer_type_name, Type::TYPE_REFERENCE, false, src_ref);
75 6 pointer_type = pointer_owned.get();
76 6 pointer_owned->complete_pointer_definition(_type, src_ref);
77 6 define_type(std::move(pointer_owned));
78 6 return pointer_type;
79 8 }
80
81 const Type *
82 2 Types::get_array_of(const Type *_type, size_t _length, const SourceReference & src_ref)
83 {
84 6 std::string array_type_name = _type->get_name() + std::string("[") + std::to_string(_length) + std::string("]");
85
86 2 const Type* array_type = get_type(array_type_name);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (array_type != nullptr) {
88 return array_type;
89 }
90 2 Gyoji::owned<Type> array_owned = std::make_unique<Type>(array_type_name, Type::TYPE_ARRAY, false, src_ref);
91 2 array_type = array_owned.get();
92 2 array_owned->complete_array_definition(_type, _length, src_ref);
93 2 define_type(std::move(array_owned));
94 2 return array_type;
95 2 }
96
97 void
98 604 Types::define_type(Gyoji::owned<Type> type)
99 {
100 604 std::string type_name = type->get_name();
101 604 type_map.insert(std::pair<std::string, Gyoji::owned<Type>>(type_name, std::move(type)));
102 604 }
103
104 const std::map<std::string, Gyoji::owned<Type>> &
105 104 Types::get_types() const
106 104 { return type_map; }
107
108
109 void
110 32 Types::dump(FILE *out) const
111 {
112
2/2
✓ Branch 5 taken 604 times.
✓ Branch 6 taken 32 times.
636 for (const auto & t : type_map) {
113 604 t.second->dump(out);
114 }
115 32 }
116
117