|
18 | 18 | #include "swift/StaticMirror/BinaryScanningTool.h"
|
19 | 19 | #include "swift/StaticMirror/BinaryScanImpl.h"
|
20 | 20 | #include "swift/DependencyScan/StringUtils.h"
|
21 |
| -//#include "swift/Option/Options.h" |
22 | 21 |
|
23 | 22 | // FIXME: Code duplication with StringUtils.cpp
|
24 | 23 | namespace swift {
|
@@ -136,3 +135,119 @@ void swift_static_mirror_conformances_set_dispose(
|
136 | 135 | delete[] set->conformances;
|
137 | 136 | delete set;
|
138 | 137 | }
|
| 138 | + |
| 139 | +static swift_static_mirror_associated_type_info_set_t * |
| 140 | +convertAssociatedTypeQueryResult( |
| 141 | + const swift::reflection::AssociatedTypeCollectionResult &scanResult) { |
| 142 | + swift_static_mirror_associated_type_info_set_t *result = |
| 143 | + new swift_static_mirror_associated_type_info_set_t; |
| 144 | + result->count = scanResult.AssociatedTypeInfos.size(); |
| 145 | + result->associated_type_infos = new swift_static_mirror_associated_type_info_t |
| 146 | + [scanResult.AssociatedTypeInfos.size()]; |
| 147 | + |
| 148 | + int associatedTypeInfoIndex = 0; |
| 149 | + for (const auto &assocTypeInfo : scanResult.AssociatedTypeInfos) { |
| 150 | + swift_static_mirror_associated_type_info_s *info = |
| 151 | + new swift_static_mirror_associated_type_info_s; |
| 152 | + info->mangled_type_name = swift::c_string_utils::create_clone( |
| 153 | + assocTypeInfo.MangledTypeName.c_str()); |
| 154 | + info->type_alias_set = new swift_static_mirror_type_alias_set_t; |
| 155 | + info->type_alias_set->count = assocTypeInfo.AssociatedTypes.size(); |
| 156 | + info->type_alias_set->type_aliases = |
| 157 | + new swift_static_mirror_type_alias_t[assocTypeInfo.AssociatedTypes |
| 158 | + .size()]; |
| 159 | + int typealiasIndex = 0; |
| 160 | + for (const auto &typeAliasInfo : assocTypeInfo.AssociatedTypes) { |
| 161 | + swift_static_mirror_type_alias_s *typealiasDetails = |
| 162 | + new swift_static_mirror_type_alias_s; |
| 163 | + typealiasDetails->type_alias_name = swift::c_string_utils::create_clone( |
| 164 | + typeAliasInfo.TypeAliasName.c_str()); |
| 165 | + typealiasDetails->substituted_type_name = |
| 166 | + swift::c_string_utils::create_clone( |
| 167 | + typeAliasInfo.SubstitutedTypeFullyQualifiedName.c_str()); |
| 168 | + typealiasDetails->substituted_type_mangled_name = |
| 169 | + swift::c_string_utils::create_clone( |
| 170 | + typeAliasInfo.SubstitutedTypeMangledName.c_str()); |
| 171 | + info->type_alias_set->type_aliases[typealiasIndex] = |
| 172 | + typealiasDetails; |
| 173 | + typealiasIndex += 1; |
| 174 | + } |
| 175 | + result->associated_type_infos[associatedTypeInfoIndex] = info; |
| 176 | + associatedTypeInfoIndex += 1; |
| 177 | + } |
| 178 | + return result; |
| 179 | +} |
| 180 | + |
| 181 | +swift_static_mirror_associated_type_info_set_t * |
| 182 | +swift_static_mirror_associated_type_info_set_create( |
| 183 | + swift_static_mirror_t static_mirror, const char *forTypeName) { |
| 184 | + BinaryScanningTool *scanTool = unwrap(static_mirror); |
| 185 | + auto scanResult = scanTool->collectAssociatedTypes(forTypeName); |
| 186 | + return convertAssociatedTypeQueryResult(scanResult); |
| 187 | +} |
| 188 | + |
| 189 | +/// Identify and collect associated types of all discovered types. |
| 190 | +swift_static_mirror_associated_type_info_set_t * |
| 191 | +swift_static_mirror_all_associated_type_info_set_create( |
| 192 | + swift_static_mirror_t static_mirror) { |
| 193 | + BinaryScanningTool *scanTool = unwrap(static_mirror); |
| 194 | + auto scanResult = scanTool->collectAllAssociatedTypes(); |
| 195 | + return convertAssociatedTypeQueryResult(scanResult); |
| 196 | +} |
| 197 | + |
| 198 | +// swift_static_mirror_associated_type query methods |
| 199 | +swift_static_mirror_string_ref_t |
| 200 | +swift_static_mirror_type_alias_get_type_alias_name( |
| 201 | + swift_static_mirror_type_alias_t type_alias) { |
| 202 | + return type_alias->type_alias_name; |
| 203 | +} |
| 204 | +swift_static_mirror_string_ref_t |
| 205 | +swift_static_mirror_type_alias_get_substituted_type_name( |
| 206 | + swift_static_mirror_type_alias_t type_alias) { |
| 207 | + return type_alias->substituted_type_name; |
| 208 | +} |
| 209 | +swift_static_mirror_string_ref_t |
| 210 | +swift_static_mirror_type_alias_get_substituted_type_mangled_name( |
| 211 | + swift_static_mirror_type_alias_t type_alias) { |
| 212 | + return type_alias->substituted_type_mangled_name; |
| 213 | +} |
| 214 | + |
| 215 | +// swift_static_mirror_associated_type_info query methods |
| 216 | +swift_static_mirror_string_ref_t |
| 217 | +swift_static_mirror_associated_type_info_get_mangled_type_name( |
| 218 | + swift_static_mirror_associated_type_info_t associated_type_info) { |
| 219 | + return associated_type_info->mangled_type_name; |
| 220 | +} |
| 221 | +swift_static_mirror_type_alias_set_t* |
| 222 | +swift_static_mirror_associated_type_info_get_type_alias_set( |
| 223 | + swift_static_mirror_associated_type_info_t associated_type_info) { |
| 224 | + return associated_type_info->type_alias_set; |
| 225 | +} |
| 226 | + |
| 227 | +void swift_static_mirror_type_alias_dispose(swift_static_mirror_type_alias_t type_alias) { |
| 228 | + swift_static_mirror_string_dispose(type_alias->substituted_type_mangled_name); |
| 229 | + swift_static_mirror_string_dispose(type_alias->substituted_type_name); |
| 230 | + swift_static_mirror_string_dispose(type_alias->type_alias_name); |
| 231 | + delete type_alias; |
| 232 | +} |
| 233 | + |
| 234 | +void swift_static_mirror_type_alias_set_dispose(swift_static_mirror_type_alias_set_t *type_alias_set) { |
| 235 | + for (size_t i = 0; i < type_alias_set->count; ++i) { |
| 236 | + swift_static_mirror_type_alias_dispose(type_alias_set->type_aliases[i]); |
| 237 | + } |
| 238 | + delete[] type_alias_set->type_aliases; |
| 239 | + delete type_alias_set; |
| 240 | +} |
| 241 | + |
| 242 | +void swift_static_mirror_associated_type_info_dispose(swift_static_mirror_associated_type_info_t associated_type_info) { |
| 243 | + swift_static_mirror_string_dispose(associated_type_info->mangled_type_name); |
| 244 | + swift_static_mirror_type_alias_set_dispose(associated_type_info->type_alias_set); |
| 245 | +} |
| 246 | + |
| 247 | +void swift_static_mirror_associated_type_info_set_dispose(swift_static_mirror_associated_type_info_set_t *associated_type_info_set) { |
| 248 | + for (size_t i = 0; i < associated_type_info_set->count; ++i) { |
| 249 | + swift_static_mirror_associated_type_info_dispose(associated_type_info_set->associated_type_infos[i]); |
| 250 | + } |
| 251 | + delete[] associated_type_info_set->associated_type_infos; |
| 252 | + delete associated_type_info_set; |
| 253 | +} |
0 commit comments