• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 13955121336

19 Mar 2025 07:30PM UTC coverage: 88.842%. First build
13955121336

push

github

web-flow
Merge PR #167 from tudasc/devel

1174 of 1361 new or added lines in 49 files covered. (86.26%)

4212 of 4741 relevant lines covered (88.84%)

261950.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.36
/lib/typelib/TypeDB.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "TypeDB.h"
14

15
#include "TypeIO.h"
16
#include "support/Logger.h"
17
#include "typelib/TypeInterface.h"
18

19
#include <ccomplex>
20
#include <complex>
21
#include <cstddef>
22
#include <iostream>
23
#include <string_view>
24
#include <utility>
25

26
namespace typeart {
27

28
namespace builtins {
29

30
namespace {
31
inline constexpr auto size_complex_float       = sizeof(std::complex<float>);
32
inline constexpr auto size_complex_double      = sizeof(std::complex<double>);
33
inline constexpr auto size_complex_long_double = sizeof(std::complex<long double>);
34
}  // namespace
35

36
#define FOR_EACH_TYPEART_BUILTIN(X)                             \
37
  X(TYPEART_UNKNOWN_TYPE, "typeart_unknown_type", 0)            \
38
  X(TYPEART_POINTER, "ptr", sizeof(void*))                      \
39
  X(TYPEART_VTABLE_POINTER, "vtable_ptr", sizeof(void*))        \
40
  X(TYPEART_VOID, "void*", sizeof(void*))                       \
41
  X(TYPEART_NULLPOINTER, "nullptr_t", sizeof(void*))            \
42
  X(TYPEART_BOOL, "bool", sizeof(bool))                         \
43
  X(TYPEART_CHAR_8, "char", sizeof(char))                       \
44
  X(TYPEART_UCHAR_8, "unsigned char", sizeof(unsigned char))    \
45
  X(TYPEART_UTF_CHAR_8, "char8_t", sizeof(char))                \
46
  X(TYPEART_UTF_CHAR_16, "char16_t", sizeof(char16_t))          \
47
  X(TYPEART_UTF_CHAR_32, "char32_t", sizeof(char32_t))          \
48
  X(TYPEART_INT_8, "int8_t", sizeof(int8_t))                    \
49
  X(TYPEART_INT_16, "short", sizeof(int16_t))                   \
50
  X(TYPEART_INT_32, "int", sizeof(int32_t))                     \
51
  X(TYPEART_INT_64, "long int", sizeof(int64_t))                \
52
  X(TYPEART_INT_128, "int128_t", 16)                            \
53
  X(TYPEART_UINT_8, "uint8_t", sizeof(uint8_t))                 \
54
  X(TYPEART_UINT_16, "unsigned short", sizeof(uint16_t))        \
55
  X(TYPEART_UINT_32, "unsigned int", sizeof(uint32_t))          \
56
  X(TYPEART_UINT_64, "unsigned long int", sizeof(uint64_t))     \
57
  X(TYPEART_UINT_128, "uint128_t", 16)                          \
58
  X(TYPEART_FLOAT_8, "float8_t", 1)                             \
59
  X(TYPEART_FLOAT_16, "float16_t", 2)                           \
60
  X(TYPEART_FLOAT_32, "float", sizeof(float))                   \
61
  X(TYPEART_FLOAT_64, "double", sizeof(double))                 \
62
  X(TYPEART_FLOAT_128, "long double", sizeof(long double))      \
63
  X(TYPEART_COMPLEX_64, "float complex", size_complex_float)    \
64
  X(TYPEART_COMPLEX_128, "double complex", size_complex_double) \
65
  X(TYPEART_COMPLEX_256, "long double complex", size_complex_long_double)
66

67
#define TYPENAME(enum_name, str_name, size) std::string{str_name},
68
#define SIZE(enum_name, str_name, size)     (size),
69
BuiltInQuery::BuiltInQuery() : names{FOR_EACH_TYPEART_BUILTIN(TYPENAME)}, sizes{FOR_EACH_TYPEART_BUILTIN(SIZE)} {
3,920✔
70
}
3,920✔
71
#undef SIZE
72
#undef TYPENAME
73

74
}  // namespace builtins
1,958✔
75

76
std::pair<std::unique_ptr<TypeDatabase>, std::error_code> make_database(std::string_view file) {
24✔
77
  auto type_db = std::make_unique<TypeDB>();
24✔
78
  auto loaded  = io::load(type_db.get(), file.data());
24✔
79
  if (!loaded) {
24✔
80
    LOG_DEBUG("Database file not found: " << file)
81
  }
12✔
82
  return {std::move(type_db), loaded.getError()};
24✔
83
}
24✔
84

85
using namespace builtins;
86

87
const std::string unknown_struck_name{"typeart_unknown_struct"};
3,650✔
88

89
void TypeDB::clear() {
2,168✔
90
  struct_info_vec.clear();
2,168✔
91
  typeid_to_list_index.clear();
2,168✔
92
  // reverseTypeMap.clear();
93
}
2,168✔
94

95
bool TypeDB::isBuiltinType(int type_id) const {
1,099,593✔
96
  return BuiltInQuery::is_builtin_type(type_id);
1,099,593✔
97
}
98

99
bool TypeDB::isReservedType(int type_id) const {
463,428✔
100
  return BuiltInQuery::is_reserved_type(type_id);
463,428✔
101
}
102

103
bool TypeDB::isStructType(int type_id) const {
54,624✔
104
  return BuiltInQuery::is_userdef_type(type_id);
54,624✔
105
}
106

107
bool TypeDB::isUnknown(int type_id) const {
80✔
108
  return BuiltInQuery::is_unknown_type(type_id);
80✔
109
}
110

NEW
111
bool TypeDB::isPointerType(int type_id) const {
×
NEW
112
  return type_id == TYPEART_VOID || type_id == TYPEART_POINTER;
×
113
}
114

115
bool TypeDB::isUserDefinedType(int type_id) const {
527✔
116
  const auto* structInfo = getStructInfo(type_id);
527✔
117
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
118
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED))
119
                             << " " << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION)))
120
  return (structInfo != nullptr) &&
1,054✔
121
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::USER_DEFINED) ||
527✔
NEW
122
          static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION));
×
123
}
124

125
bool TypeDB::isVectorType(int type_id) const {
42✔
126
  const auto* structInfo = getStructInfo(type_id);
42✔
127
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
128
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR)))
129
  return (structInfo != nullptr) &&
84✔
130
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR));
42✔
131
}
132

NEW
133
bool TypeDB::isUnion(int type_id) const {
×
NEW
134
  const auto* structInfo = getStructInfo(type_id);
×
135
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
136
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION)))
NEW
137
  return (structInfo != nullptr) && (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION));
×
138
}
139

140
bool TypeDB::isValid(int type_id) const {
234,167✔
141
  if (isBuiltinType(type_id)) {
234,167✔
142
    return true;
202,275✔
143
  }
144
  return typeid_to_list_index.find(type_id) != typeid_to_list_index.end();
31,892✔
145
}
234,167✔
146

147
void TypeDB::registerStruct(const StructTypeInfo& struct_type, bool overwrite) {
11,511✔
148
  if (isValid(struct_type.type_id) || !isStructType(struct_type.type_id)) {
11,511✔
149
    if (isBuiltinType(struct_type.type_id)) {
80✔
150
      LOG_ERROR("Built-in type ID used for struct " << struct_type.name);
12✔
151
    } else if (isReservedType(struct_type.type_id)) {
80✔
152
      LOG_ERROR("Type ID is reserved for builtin types. Struct: " << struct_type.name);
24✔
153
    } else if (isUnknown(struct_type.type_id)) {
68✔
154
      LOG_ERROR("Type ID is reserved for unknown types. Struct: " << struct_type.name);
×
155
    } else {
×
156
      if (!overwrite) {
44✔
157
        LOG_ERROR("Struct type ID already registered for " << struct_type.name << ". Conflicting struct is "
×
158
                                                           << getStructInfo(struct_type.type_id)->name);
159
        return;
×
160
      }
161
      LOG_DEBUG("Overwrite struct " << struct_type.name)
162
      auto& info = *getStructInfo(struct_type.type_id);
44✔
163
      info       = struct_type;
44✔
164
    }
165
    return;
80✔
166
  }
167

168
  struct_info_vec.push_back(struct_type);
11,431✔
169
  typeid_to_list_index.insert({struct_type.type_id, struct_info_vec.size() - 1});
11,431✔
170
}
11,511✔
171

172
const std::string& TypeDB::getTypeName(int type_id) const {
447,191✔
173
  if (isBuiltinType(type_id)) {
447,191✔
174
    return builtins.get_name(type_id);
404,322✔
175
  }
176
  if (isStructType(type_id)) {
42,869✔
177
    const auto* structInfo = getStructInfo(type_id);
42,797✔
178
    if (structInfo != nullptr) {
42,797✔
179
      return structInfo->name;
42,725✔
180
    }
181
  }
72✔
182

183
  return unknown_struck_name;
144✔
184
}
447,191✔
185

186
size_t TypeDB::getTypeSize(int type_id) const {
463,310✔
187
  if (isReservedType(type_id)) {
463,310✔
188
    if (isBuiltinType(type_id)) {
417,608✔
189
      return builtins.get_size(type_id);
417,572✔
190
    }
191
    return 0;
36✔
192
  }
193

194
  const auto* structInfo = getStructInfo(type_id);
45,702✔
195
  if (structInfo != nullptr) {
45,702✔
196
    return structInfo->extent;
45,654✔
197
  }
198
  return 0;
48✔
199
}
463,310✔
200

201
const StructTypeInfo* TypeDB::getStructInfo(int type_id) const {
89,668✔
202
  const auto index_iter = typeid_to_list_index.find(type_id);
89,668✔
203
  if (index_iter != typeid_to_list_index.end()) {
89,668✔
204
    return &struct_info_vec[index_iter->second];
89,524✔
205
  }
206
  return nullptr;
144✔
207
}
89,668✔
208

209
StructTypeInfo* TypeDB::getStructInfo(int type_id) {
5,508✔
210
  const auto index_iter = typeid_to_list_index.find(type_id);
5,508✔
211
  if (index_iter != typeid_to_list_index.end()) {
5,508✔
212
    return &struct_info_vec[index_iter->second];
5,496✔
213
  }
214
  return nullptr;
12✔
215
}
5,508✔
216

217
const std::vector<StructTypeInfo>& TypeDB::getStructList() const {
5,394✔
218
  return struct_info_vec;
5,394✔
219
}
220

221
}  // namespace typeart
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc