GCC Code Coverage Report


Directory: src/
File: src/mir/types.cpp
Date: 2025-10-24 11:14:59
Exec Total Coverage
Lines: 53 64 82.8%
Functions: 8 9 88.9%
Branches: 8 10 80.0%

Line Branch Exec Source
1 /* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gyoji-mir/types.hpp>
16 #include <variant>
17 #include <stdio.h>
18
19 using namespace Gyoji::context;
20 using namespace Gyoji::mir;
21
22 ////////////////////////////////////////
23 // Types
24 ////////////////////////////////////////
25
26 const static std::string zero_source_filename("builtin");
27 static SourceReference zero_source_ref(zero_source_filename, 0, 0, 0);
28
29 30 Types::Types()
30 {
31 // XXX Definitely not the place to do this, but
32 // it's a good enough place for now that I don't care
33 // until we have a type system we can plug into this
34
35 30 define_type(std::make_unique<Type>("i8", Type::TYPE_PRIMITIVE_i8, true, zero_source_ref));
36 30 define_type(std::make_unique<Type>("i16", Type::TYPE_PRIMITIVE_i16, true, zero_source_ref));
37 30 define_type(std::make_unique<Type>("i32", Type::TYPE_PRIMITIVE_i32, true, zero_source_ref));
38 30 define_type(std::make_unique<Type>("i64", Type::TYPE_PRIMITIVE_i64, true, zero_source_ref));
39
40 30 define_type(std::make_unique<Type>("u8", Type::TYPE_PRIMITIVE_u8, true, zero_source_ref));
41 30 define_type(std::make_unique<Type>("u16", Type::TYPE_PRIMITIVE_u16, true, zero_source_ref));
42 30 define_type(std::make_unique<Type>("u32", Type::TYPE_PRIMITIVE_u32, true, zero_source_ref));
43 30 define_type(std::make_unique<Type>("u64", Type::TYPE_PRIMITIVE_u64, true, zero_source_ref));
44
45 30 define_type(std::make_unique<Type>("f32", Type::TYPE_PRIMITIVE_f32, true, zero_source_ref));
46 30 define_type(std::make_unique<Type>("f64", Type::TYPE_PRIMITIVE_f64, true, zero_source_ref));
47
48 30 define_type(std::make_unique<Type>("bool", Type::TYPE_PRIMITIVE_bool, true, zero_source_ref));
49 30 define_type(std::make_unique<Type>("void", Type::TYPE_PRIMITIVE_void, true, zero_source_ref));
50 30 }
51
52 30 Types::~Types()
53 30 {}
54
55 Type *
56 3470 Types::get_type(std::string type) const
57 {
58 3470 const auto & it = type_map.find(type);
59
2/2
✓ Branch 2 taken 770 times.
✓ Branch 3 taken 2700 times.
3470 if (it == type_map.end()) {
60 770 return nullptr;
61 }
62 2700 return it->second.get();
63 }
64 const Type *
65 164 Types::get_pointer_to(const Type *_type, const SourceReference & src_ref)
66 {
67 164 std::string pointer_type_name = _type->get_name() + std::string("*");
68 164 const Type* pointer_type = get_type(pointer_type_name);
69
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 66 times.
164 if (pointer_type != nullptr) {
70 98 return pointer_type;
71 }
72 66 Gyoji::owned<Type> pointer_owned = std::make_unique<Type>(pointer_type_name, Type::TYPE_POINTER, false, src_ref);
73 66 pointer_type = pointer_owned.get();
74 66 pointer_owned->complete_pointer_definition(_type, src_ref);
75 66 define_type(std::move(pointer_owned));
76 66 return pointer_type;
77 164 }
78
79 const Type *
80 8 Types::get_reference_to(const Type *_type, const SourceReference & src_ref)
81 {
82 8 std::string pointer_type_name = _type->get_name() + std::string("&");
83
84 8 const Type* pointer_type = get_type(pointer_type_name);
85
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (pointer_type != nullptr) {
86 2 return pointer_type;
87 }
88 6 Gyoji::owned<Type> pointer_owned = std::make_unique<Type>(pointer_type_name, Type::TYPE_REFERENCE, false, src_ref);
89 6 pointer_type = pointer_owned.get();
90 6 pointer_owned->complete_pointer_definition(_type, src_ref);
91 6 define_type(std::move(pointer_owned));
92 6 return pointer_type;
93 8 }
94
95 const Type *
96 Types::get_array_of(
97 const Type *_type,
98 size_t _length,
99 const SourceReference & _src_ref)
100 {
101 std::string array_type_name = _type->get_name() + std::string("[") + std::to_string(_length) + std::string("]");
102
103 const Type* array_type = get_type(array_type_name);
104 if (array_type != nullptr) {
105 return array_type;
106 }
107 Gyoji::owned<Type> array_owned = std::make_unique<Type>(array_type_name, Type::TYPE_ARRAY, false, _src_ref);
108 array_type = array_owned.get();
109 array_owned->complete_array_definition(_type, _length, _src_ref);
110 define_type(std::move(array_owned));
111 return array_type;
112 }
113
114 void
115 580 Types::define_type(Gyoji::owned<Type> type)
116 {
117 580 std::string type_name = type->get_name();
118 580 type_map.insert(std::pair<std::string, Gyoji::owned<Type>>(type_name, std::move(type)));
119 580 }
120
121 const std::map<std::string, Gyoji::owned<Type>> &
122 104 Types::get_types() const
123 104 { return type_map; }
124
125
126 void
127 30 Types::dump(FILE *out) const
128 {
129
2/2
✓ Branch 5 taken 580 times.
✓ Branch 6 taken 30 times.
610 for (const auto & t : type_map) {
130 580 t.second->dump(out);
131 }
132 30 }
133
134