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

tudasc / TypeART / 24397737785

14 Apr 2026 12:00PM UTC coverage: 90.264% (+1.3%) from 88.924%
24397737785

Pull #187

github

web-flow
Merge 278119205 into de3bc148a
Pull Request #187: Release v2.2

880 of 935 new or added lines in 31 files covered. (94.12%)

17 existing lines in 3 files now uncovered.

4886 of 5413 relevant lines covered (90.26%)

38873.79 hits per line

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

78.35
/lib/typelib/TypeDB.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2026 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
  X(TYPEART_WCHAR, "wchar_t", sizeof(wchar_t))
67

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

1,970✔
75
}  // namespace builtins
76

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

86
using namespace builtins;
87

88
const std::string unknown_struct_name{"typeart_unknown_struct"};
6,602✔
89

90
void TypeDB::clear() {
1,848✔
91
  struct_info_vec.clear();
1,848✔
92
  typeid_to_list_index.clear();
1,848✔
93
  // reverseTypeMap.clear();
94
}
1,848✔
95

96
bool TypeDB::isBuiltinType(int type_id) const {
2,056,803✔
97
  return BuiltInQuery::is_builtin_type(type_id);
2,056,803✔
98
}
99

100
bool TypeDB::isReservedType(int type_id) const {
834,629✔
101
  return BuiltInQuery::is_reserved_type(type_id);
834,629✔
102
}
103

104
bool TypeDB::isStructType(int type_id) const {
141,627✔
105
  return BuiltInQuery::is_userdef_type(type_id);
141,627✔
106
}
107

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

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

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

129
bool TypeDB::isVectorType(int type_id) const {
60✔
130
  const auto* structInfo = getStructInfo(type_id);
60✔
131
  LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
132
                             << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR)))
133
  return (structInfo != nullptr) &&
120✔
134
         (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::LLVM_VECTOR));
60✔
135
}
136

137
bool TypeDB::isUnion(int type_id) const {
×
138
  const auto* structInfo = getStructInfo(type_id);
×
NEW
139
  if (structInfo != nullptr) {
×
140
    LOG_DEBUG(structInfo->name << " " << static_cast<int>(structInfo->flag) << " "
141
                               << (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION)))
NEW
142
  }
×
UNCOV
143
  return (structInfo != nullptr) && (static_cast<int>(structInfo->flag) == static_cast<int>(StructTypeFlag::UNION));
×
144
}
145

146
bool TypeDB::isValid(int type_id) const {
421,625✔
147
  if (isBuiltinType(type_id)) {
421,625✔
148
    return true;
354,113✔
149
  }
150
  return typeid_to_list_index.find(type_id) != typeid_to_list_index.end();
67,512✔
151
}
421,625✔
152

153
void TypeDB::registerStruct(const StructTypeInfo& struct_type, bool overwrite) {
15,413✔
154
  if (isValid(struct_type.type_id) || !isStructType(struct_type.type_id)) {
15,413✔
155
    if (isBuiltinType(struct_type.type_id)) {
52✔
UNCOV
156
      LOG_ERROR("Built-in type ID used for struct " << struct_type.name);
×
157
    } else if (isReservedType(struct_type.type_id)) {
52✔
UNCOV
158
      LOG_ERROR("Type ID is reserved for builtin types. Struct: " << struct_type.name);
×
159
    } else if (isUnknown(struct_type.type_id)) {
52✔
160
      LOG_ERROR("Type ID is reserved for unknown types. Struct: " << struct_type.name);
×
161
    } else {
×
162
      if (!overwrite) {
52✔
163
        LOG_DEBUG("Struct type ID already registered for " << struct_type.name << ". Conflicting struct is "
164
                                                           << getStructInfo(struct_type.type_id)->name);
165
        return;
×
166
      }
167
      LOG_DEBUG("Overwrite struct " << struct_type.name)
168
      auto& info = *getStructInfo(struct_type.type_id);
52✔
169
      info       = struct_type;
52✔
170
    }
171
    return;
52✔
172
  }
173

174
  struct_info_vec.push_back(struct_type);
15,361✔
175
  typeid_to_list_index.insert({struct_type.type_id, struct_info_vec.size() - 1});
15,361✔
176
}
15,413✔
177

178
const std::string& TypeDB::getTypeName(int type_id) const {
855,429✔
179
  if (isBuiltinType(type_id)) {
855,429✔
180
    return builtins.get_name(type_id);
729,835✔
181
  }
182
  if (isStructType(type_id)) {
125,594✔
183
    const auto* structInfo = getStructInfo(type_id);
125,510✔
184
    if (structInfo != nullptr) {
125,510✔
185
      return structInfo->name;
125,375✔
186
    }
187
  }
135✔
188

189
  return unknown_struct_name;
219✔
190
}
855,429✔
191

192
size_t TypeDB::getTypeSize(int type_id) const {
834,495✔
193
  if (isReservedType(type_id)) {
834,495✔
194
    if (isBuiltinType(type_id)) {
734,200✔
195
      return builtins.get_size(type_id);
734,137✔
196
    }
197
    return 0;
63✔
198
  }
199

200
  const auto* structInfo = getStructInfo(type_id);
100,295✔
201
  if (structInfo != nullptr) {
100,295✔
202
    return structInfo->extent;
100,214✔
203
  }
204
  return 0;
81✔
205
}
834,495✔
206

207
const StructTypeInfo* TypeDB::getStructInfo(int type_id) const {
258,407✔
208
  const auto index_iter = typeid_to_list_index.find(type_id);
258,407✔
209
  if (index_iter != typeid_to_list_index.end()) {
258,407✔
210
    return &struct_info_vec[index_iter->second];
258,149✔
211
  }
212
  return nullptr;
258✔
213
}
258,407✔
214

215
StructTypeInfo* TypeDB::getStructInfo(int type_id) {
8,131✔
216
  const auto index_iter = typeid_to_list_index.find(type_id);
8,131✔
217
  if (index_iter != typeid_to_list_index.end()) {
8,131✔
218
    return &struct_info_vec[index_iter->second];
8,131✔
219
  }
UNCOV
220
  return nullptr;
×
221
}
8,131✔
222

223
const std::vector<StructTypeInfo>& TypeDB::getStructList() const {
7,249✔
224
  return struct_info_vec;
7,249✔
225
}
226

227
}  // 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