1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "Private.h"
#include "swift/ABI/TypeIdentity.h"
#include "swift/Basic/Range.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/Portability.h"
#include "swift/Strings.h"
#include <vector>
#include <inttypes.h>
#if SWIFT_OBJC_INTEROP
#include <objc/runtime.h>
#endif
using namespace swift;
Demangle::NodePointer
swift::_buildDemanglingForContext(const ContextDescriptor *context,
llvm::ArrayRef<NodePointer> demangledGenerics,
Demangle::Demangler &Dem) {
unsigned usedDemangledGenerics = 0;
NodePointer node = nullptr;
// Walk up the context tree.
llvm::SmallVector<const ContextDescriptor *, 8> descriptorPath;
{
const ContextDescriptor *parent = context;
while (parent) {
descriptorPath.push_back(parent);
parent = parent->Parent;
}
}
auto getGenericArgsTypeListForContext =
[&](const ContextDescriptor *context) -> NodePointer {
if (demangledGenerics.empty())
return nullptr;
if (context->getKind() == ContextDescriptorKind::Anonymous)
return nullptr;
auto generics = context->getGenericContext();
if (!generics)
return nullptr;
auto numParams = generics->getGenericContextHeader().NumParams;
if (numParams <= usedDemangledGenerics)
return nullptr;
auto genericArgsList = Dem.createNode(Node::Kind::TypeList);
for (unsigned e = generics->getGenericContextHeader().NumParams;
usedDemangledGenerics < e;
++usedDemangledGenerics) {
genericArgsList->addChild(demangledGenerics[usedDemangledGenerics],
Dem);
}
return genericArgsList;
};
for (auto component : llvm::reverse(descriptorPath)) {
switch (auto kind = component->getKind()) {
case ContextDescriptorKind::Module: {
assert(node == nullptr && "module should be top level");
auto name = llvm::cast<ModuleContextDescriptor>(component)->Name.get();
node = Dem.createNode(Node::Kind::Module, name);
break;
}
case ContextDescriptorKind::Extension: {
auto extension = llvm::cast<ExtensionContextDescriptor>(component);
// Demangle the extension self type.
auto selfType = Dem.demangleType(extension->getMangledExtendedContext(),
ResolveToDemanglingForContext(Dem));
if (selfType->getKind() == Node::Kind::Type)
selfType = selfType->getChild(0);
// Substitute in the generic arguments.
auto genericArgsList = getGenericArgsTypeListForContext(component);
if (selfType->getKind() == Node::Kind::BoundGenericEnum
|| selfType->getKind() == Node::Kind::BoundGenericStructure
|| selfType->getKind() == Node::Kind::BoundGenericClass
|| selfType->getKind() == Node::Kind::BoundGenericOtherNominalType) {
if (genericArgsList) {
auto substSelfType = Dem.createNode(selfType->getKind());
substSelfType->addChild(selfType->getChild(0), Dem);
substSelfType->addChild(genericArgsList, Dem);
selfType = substSelfType;
} else {
// TODO: Use the unsubstituted type if we can't handle the
// substitutions yet.
selfType = selfType->getChild(0)->getChild(0);
}
}
auto extNode = Dem.createNode(Node::Kind::Extension);
extNode->addChild(node, Dem);
extNode->addChild(selfType, Dem);
// TODO: Turn the generic signature into a demangling as the third
// generic argument.
node = extNode;
break;
}
case ContextDescriptorKind::Protocol: {
auto protocol = llvm::cast<ProtocolDescriptor>(component);
auto name = protocol->Name.get();
auto protocolNode = Dem.createNode(Node::Kind::Protocol);
protocolNode->addChild(node, Dem);
auto nameNode = Dem.createNode(Node::Kind::Identifier, name);
protocolNode->addChild(nameNode, Dem);
node = protocolNode;
break;
}
default:
// Form a type context demangling for type contexts.
if (auto type = llvm::dyn_cast<TypeContextDescriptor>(component)) {
auto identity = ParsedTypeIdentity::parse(type);
Node::Kind nodeKind;
Node::Kind genericNodeKind;
switch (kind) {
case ContextDescriptorKind::Class:
nodeKind = Node::Kind::Class;
genericNodeKind = Node::Kind::BoundGenericClass;
break;
case ContextDescriptorKind::Struct:
nodeKind = Node::Kind::Structure;
genericNodeKind = Node::Kind::BoundGenericStructure;
break;
case ContextDescriptorKind::Enum:
nodeKind = Node::Kind::Enum;
genericNodeKind = Node::Kind::BoundGenericEnum;
break;
default:
// We don't know about this kind of type. Use an "other type" mangling
// for it.
nodeKind = Node::Kind::OtherNominalType;
genericNodeKind = Node::Kind::BoundGenericOtherNominalType;
break;
}
// Override the node kind if this is a Clang-imported type so we give it
// a stable mangling.
if (identity.isCTypedef()) {
nodeKind = Node::Kind::TypeAlias;
} else if (nodeKind != Node::Kind::Structure &&
_isCImportedTagType(type, identity)) {
nodeKind = Node::Kind::Structure;
}
auto typeNode = Dem.createNode(nodeKind);
typeNode->addChild(node, Dem);
auto nameNode = Dem.createNode(Node::Kind::Identifier,
identity.getABIName());
if (identity.isAnyRelatedEntity()) {
auto kindNode = Dem.createNode(Node::Kind::Identifier,
identity.getRelatedEntityName());
auto relatedName = Dem.createNode(Node::Kind::RelatedEntityDeclName);
relatedName->addChild(kindNode, Dem);
relatedName->addChild(nameNode, Dem);
nameNode = relatedName;
}
typeNode->addChild(nameNode, Dem);
node = typeNode;
// Apply generic arguments if the context is generic.
if (auto genericArgsList = getGenericArgsTypeListForContext(component)){
auto unspecializedType = Dem.createNode(Node::Kind::Type);
unspecializedType->addChild(node, Dem);
auto genericNode = Dem.createNode(genericNodeKind);
genericNode->addChild(unspecializedType, Dem);
genericNode->addChild(genericArgsList, Dem);
node = genericNode;
}
break;
}
// This runtime doesn't understand this context, or it's a context with
// no richer runtime information available about it (such as an anonymous
// context). Use an unstable mangling to represent the context by its
// pointer identity.
char addressBuf[sizeof(void*) * 2 + 1 + 1];
snprintf(addressBuf, sizeof(addressBuf), "$%" PRIxPTR, (uintptr_t)component);
auto anonNode = Dem.createNode(Node::Kind::AnonymousContext);
CharVector addressStr;
addressStr.append(addressBuf, Dem);
auto name = Dem.createNode(Node::Kind::Identifier, addressStr);
anonNode->addChild(name, Dem);
anonNode->addChild(node, Dem);
// Collect generic arguments if the context is generic.
auto genericArgsList = getGenericArgsTypeListForContext(component);
if (!genericArgsList)
genericArgsList = Dem.createNode(Node::Kind::TypeList);
anonNode->addChild(genericArgsList, Dem);
node = anonNode;
break;
}
}
// Wrap the final result in a top-level Type node.
auto top = Dem.createNode(Node::Kind::Type);
top->addChild(node, Dem);
return top;
}
// FIXME: This stuff should be merged with the existing logic in
// include/swift/RemoteInspection/TypeRefBuilder.h as part of the rewrite
// to change stdlib reflection over to using remote mirrors.
Demangle::NodePointer
swift::_swift_buildDemanglingForMetadata(const Metadata *type,
Demangle::Demangler &Dem);
static Demangle::NodePointer
_buildDemanglerForBuiltinType(const Metadata *type, Demangle::Demangler &Dem) {
#define BUILTIN_TYPE(Symbol, Name) \
if (type == &METADATA_SYM(Symbol).base) \
return Dem.createNode(Node::Kind::BuiltinTypeName, Name);
#if !SWIFT_STDLIB_ENABLE_VECTOR_TYPES
#define BUILTIN_VECTOR_TYPE(ElementSymbol, ElementName, Width)
#endif
#include "swift/Runtime/BuiltinTypes.def"
return nullptr;
}
// Build a demangled type tree for a type pack.
static Demangle::NodePointer
_buildDemanglingForMetadataPack(MetadataPackPointer pack, size_t count,
Demangle::Demangler &Dem) {
using namespace Demangle;
auto node = Dem.createNode(Node::Kind::Pack);
for (size_t i = 0; i < count; ++i) {
auto elt = _swift_buildDemanglingForMetadata(pack.getElements()[i], Dem);
if (elt == nullptr)
return nullptr;
node->addChild(elt, Dem);
}
return node;
}
/// Build an array of demangling trees for each generic argument to the given
/// generic type context descriptor.
///
/// Note:
/// - The input array has an entry for those generic parameter descriptors which
/// are key arguments only.
/// - The output array has an entry for each generic parameter descriptor,
/// whether or not it is a key argument.
///
/// The generic parameters which are not key arguments were made non-canonical
/// by constraining them to a concrete type or another generic parameter.
///
/// We figure out their type by looking at the same-type requirements of the
/// generic context. We demangle their type from the requirement, using the
/// generic arguments area as the substitution map; this gives us the metadata
/// for their argument. Then we convert the metadata to a mangling.
///
/// The output array is flat; the generic parameters of each depth are
/// concatenated together.
static bool _buildDemanglingForGenericArgs(
const TypeContextDescriptor *description,
const Metadata *const *genericArgs,
llvm::SmallVectorImpl<NodePointer> &demangledGenerics,
Demangle::Demangler &Dem) {
auto generics = description->getGenericContext();
if (!generics)
return true;
auto packHeader = generics->getGenericPackShapeHeader();
auto packDescriptors = generics->getGenericPackShapeDescriptors();
llvm::SmallVector<MetadataOrPack> allGenericArgs;
auto numKeyArgs = 0;
for (auto param : generics->getGenericParams()) {
if (param.hasKeyArgument()) {
numKeyArgs += 1;
}
}
// _gatherWrittenGenericParameters expects to immediately read key generic
// arguments, so skip past the shape classes if we have any.
auto nonShapeClassGenericArgs = genericArgs + packHeader.NumShapeClasses;
llvm::ArrayRef<const void *> genericArgsRef(
reinterpret_cast<const void * const *>(nonShapeClassGenericArgs), numKeyArgs);
if (!_gatherWrittenGenericParameters(description, genericArgsRef,
allGenericArgs, Dem)) {
return false;
}
auto argIndex = 0;
auto packIndex = 0;
for (auto param : generics->getGenericParams()) {
auto genericArg = allGenericArgs[argIndex];
switch (param.getKind()) {
case GenericParamKind::Type: {
auto metadata = genericArg.getMetadata();
auto genericArgDemangling =
_swift_buildDemanglingForMetadata(metadata, Dem);
if (!genericArgDemangling) {
return false;
}
demangledGenerics.push_back(genericArgDemangling);
break;
}
case GenericParamKind::TypePack: {
auto packDescriptor = packDescriptors[packIndex];
assert(packDescriptor.Kind == GenericPackKind::Metadata);
assert(packDescriptor.ShapeClass < packHeader.NumShapeClasses);
// Arg index is not interested in the shape classes, but the pack
// descriptor's index is in terms of the shape classes.
assert(packDescriptor.Index == argIndex + packHeader.NumShapeClasses);
MetadataPackPointer pack(genericArg.getMetadataPack());
size_t count = reinterpret_cast<size_t>(genericArgs[packDescriptor.ShapeClass]);
auto genericArgDemangling = _buildDemanglingForMetadataPack(pack, count, Dem);
if (genericArgDemangling == nullptr)
return false;
demangledGenerics.push_back(genericArgDemangling);
packIndex += 1;
break;
}
default:
// We don't know about this kind of parameter.
return false;
}
argIndex += 1;
}
return true;
}
/// Build a demangled type tree for a nominal type.
static Demangle::NodePointer
_buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
using namespace Demangle;
// Get the context descriptor from the type metadata.
const TypeContextDescriptor *description;
switch (type->getKind()) {
case MetadataKind::Class: {
auto classType = static_cast<const ClassMetadata *>(type);
if (!classType->isTypeMetadata()) {
return nullptr;
}
#if SWIFT_OBJC_INTEROP
// Peek through artificial subclasses.
while (classType->isArtificialSubclass())
classType = classType->Superclass;
#endif
description = classType->getDescription();
break;
}
case MetadataKind::Enum:
case MetadataKind::Optional: {
auto enumType = static_cast<const EnumMetadata *>(type);
description = enumType->Description;
break;
}
case MetadataKind::Struct: {
auto structType = static_cast<const StructMetadata *>(type);
description = structType->Description;
break;
}
case MetadataKind::ForeignClass: {
auto foreignType = static_cast<const ForeignClassMetadata *>(type);
description = foreignType->Description;
break;
}
case MetadataKind::ForeignReferenceType: {
auto foreignType = static_cast<const ForeignReferenceTypeMetadata *>(type);
description = foreignType->Description;
break;
}
default:
return nullptr;
}
llvm::SmallVector<NodePointer, 8> demangledGenerics;
if (description->isGeneric()) {
auto genericArgs = description->getGenericArguments(type);
// Gather the complete set of generic arguments that must be written to
// form this type.
if (!_buildDemanglingForGenericArgs(description, genericArgs,
demangledGenerics, Dem))
return nullptr;
}
return _buildDemanglingForContext(description, demangledGenerics, Dem);
}
// Build a demangled type tree for a type.
//
// FIXME: This should use MetadataReader.h.
Demangle::NodePointer
swift::_swift_buildDemanglingForMetadata(const Metadata *type,
Demangle::Demangler &Dem) {
using namespace Demangle;
switch (type->getKind()) {
case MetadataKind::Class:
case MetadataKind::Enum:
case MetadataKind::Optional:
case MetadataKind::Struct:
case MetadataKind::ForeignClass:
case MetadataKind::ForeignReferenceType:
return _buildDemanglingForNominalType(type, Dem);
case MetadataKind::ObjCClassWrapper: {
#if SWIFT_OBJC_INTEROP
auto objcWrapper = static_cast<const ObjCClassWrapperMetadata *>(type);
const char *className = class_getName(objcWrapper->getObjCClassObject());
auto module = Dem.createNode(Node::Kind::Module, MANGLING_MODULE_OBJC);
auto node = Dem.createNode(Node::Kind::Class);
node->addChild(module, Dem);
node->addChild(Dem.createNode(Node::Kind::Identifier,
llvm::StringRef(className)), Dem);
return node;
#else
assert(false && "no ObjC interop");
return nullptr;
#endif
}
case MetadataKind::Existential: {
auto exis = static_cast<const ExistentialTypeMetadata *>(type);
auto protocols = exis->getProtocols();
auto type_list = Dem.createNode(Node::Kind::TypeList);
auto proto_list = Dem.createNode(Node::Kind::ProtocolList);
proto_list->addChild(type_list, Dem);
// The protocol descriptors should be pre-sorted since the compiler will
// only ever make a swift_getExistentialTypeMetadata invocation using
// its canonical ordering of protocols.
for (auto protocol : protocols) {
#if SWIFT_OBJC_INTEROP
if (protocol.isObjC()) {
// The protocol name is mangled as a type symbol, with the _Tt prefix.
StringRef ProtoName(protocol.getName());
NodePointer protocolNode = Dem.demangleSymbol(ProtoName);
// ObjC protocol names aren't mangled.
if (!protocolNode) {
auto module = Dem.createNode(Node::Kind::Module,
MANGLING_MODULE_OBJC);
auto node = Dem.createNode(Node::Kind::Protocol);
node->addChild(module, Dem);
node->addChild(Dem.createNode(Node::Kind::Identifier, ProtoName),
Dem);
auto typeNode = Dem.createNode(Node::Kind::Type);
typeNode->addChild(node, Dem);
type_list->addChild(typeNode, Dem);
continue;
}
// Dig out the protocol node.
// Global -> (Protocol|TypeMangling)
protocolNode = protocolNode->getChild(0);
if (protocolNode->getKind() == Node::Kind::TypeMangling) {
protocolNode = protocolNode->getChild(0); // TypeMangling -> Type
protocolNode = protocolNode->getChild(0); // Type -> ProtocolList
protocolNode = protocolNode->getChild(0); // ProtocolList -> TypeList
protocolNode = protocolNode->getChild(0); // TypeList -> Type
assert(protocolNode->getKind() == Node::Kind::Type);
assert(protocolNode->getChild(0)->getKind() == Node::Kind::Protocol);
} else {
assert(protocolNode->getKind() == Node::Kind::Protocol);
}
type_list->addChild(protocolNode, Dem);
continue;
}
#endif
auto protocolNode =
_buildDemanglingForContext(protocol.getSwiftProtocol(), {}, Dem);
if (!protocolNode)
return nullptr;
type_list->addChild(protocolNode, Dem);
}
if (auto superclass = exis->getSuperclassConstraint()) {
// If there is a superclass constraint, we mangle it specially.
auto result = Dem.createNode(Node::Kind::ProtocolListWithClass);
auto superclassNode = _swift_buildDemanglingForMetadata(superclass, Dem);
if (!superclassNode)
return nullptr;
result->addChild(proto_list, Dem);
result->addChild(superclassNode, Dem);
return result;
}
if (exis->isClassBounded()) {
// Check if the class constraint is implied by any of our
// protocols.
bool requiresClassImplicit = false;
for (auto protocol : protocols) {
if (protocol.getClassConstraint() == ProtocolClassConstraint::Class)
requiresClassImplicit = true;
}
// If it was implied, we don't do anything special.
if (requiresClassImplicit)
return proto_list;
// If the existential type has an explicit AnyObject constraint,
// we must mangle it as such.
auto result = Dem.createNode(Node::Kind::ProtocolListWithAnyObject);
result->addChild(proto_list, Dem);
return result;
}
// Just a simple composition of protocols.
return proto_list;
}
case MetadataKind::ExtendedExistential: {
// FIXME: Implement this by demangling the extended existential and
// substituting the generalization arguments into the demangle tree.
// For now, unconditional casts will report '<<< invalid type >>>' when
// they fail.
// TODO: for clients that need to guarantee round-tripping, demangle
// to a SymbolicExtendedExistentialType.
return nullptr;
}
case MetadataKind::ExistentialMetatype: {
auto metatype = static_cast<const ExistentialMetatypeMetadata *>(type);
auto instance = _swift_buildDemanglingForMetadata(metatype->InstanceType,
Dem);
if (!instance)
return nullptr;
auto node = Dem.createNode(Node::Kind::ExistentialMetatype);
node->addChild(instance, Dem);
return node;
}
case MetadataKind::Function: {
auto func = static_cast<const FunctionTypeMetadata *>(type);
Node::Kind kind;
switch (func->getConvention()) {
case FunctionMetadataConvention::Swift:
if (!func->isEscaping())
kind = Node::Kind::NoEscapeFunctionType;
else
kind = Node::Kind::FunctionType;
break;
case FunctionMetadataConvention::Block:
kind = Node::Kind::ObjCBlock;
break;
case FunctionMetadataConvention::CFunctionPointer:
kind = Node::Kind::CFunctionPointer;
break;
case FunctionMetadataConvention::Thin:
kind = Node::Kind::ThinFunctionType;
break;
}
llvm::SmallVector<std::pair<NodePointer, bool>, 8> inputs;
for (unsigned i = 0, e = func->getNumParameters(); i < e; ++i) {
auto param = func->getParameter(i);
auto flags = func->getParameterFlags(i);
auto input = _swift_buildDemanglingForMetadata(param, Dem);
// If we failed to build the demangling for an input, we have to fail
// building the demangling for the function type too.
if (!input)
return nullptr;
auto wrapInput = [&](Node::Kind kind) {
auto parent = Dem.createNode(kind);
parent->addChild(input, Dem);
input = parent;
};
if (flags.isNoDerivative()) {
wrapInput(Node::Kind::NoDerivative);
}
switch (flags.getOwnership()) {
case ParameterOwnership::Default:
/* nothing */
break;
case ParameterOwnership::InOut:
wrapInput(Node::Kind::InOut);
break;
case ParameterOwnership::Shared:
wrapInput(Node::Kind::Shared);
break;
case ParameterOwnership::Owned:
wrapInput(Node::Kind::Owned);
break;
}
if (flags.isIsolated()) {
wrapInput(Node::Kind::Isolated);
}
if (flags.isSending()) {
wrapInput(Node::Kind::Sending);
}
inputs.push_back({input, flags.isVariadic()});
}
NodePointer totalInput = nullptr;
switch (inputs.size()) {
case 1: {
auto singleParam = inputs.front();
// If the sole unlabeled parameter has a non-tuple type, encode
// the parameter list as a single type.
if (!singleParam.second) {
auto singleType = singleParam.first;
if (singleType->getKind() == Node::Kind::Type)
singleType = singleType->getFirstChild();
if (singleType->getKind() != Node::Kind::Tuple) {
totalInput = singleParam.first;
break;
}
}
// Otherwise it requires a tuple wrapper.
SWIFT_FALLTHROUGH;
}
// This covers both none and multiple parameters.
default:
auto tuple = Dem.createNode(Node::Kind::Tuple);
for (auto &input : inputs) {
NodePointer eltType;
bool isVariadic;
std::tie(eltType, isVariadic) = input;
// Tuple element := variadic-marker label? type
auto tupleElt = Dem.createNode(Node::Kind::TupleElement);
if (isVariadic)
tupleElt->addChild(Dem.createNode(Node::Kind::VariadicMarker), Dem);
if (eltType->getKind() == Node::Kind::Type) {
tupleElt->addChild(eltType, Dem);
} else {
auto type = Dem.createNode(Node::Kind::Type);
type->addChild(eltType, Dem);
tupleElt->addChild(type, Dem);
}
tuple->addChild(tupleElt, Dem);
}
totalInput = tuple;
break;
}
NodePointer parameters = Dem.createNode(Node::Kind::ArgumentTuple);
NodePointer paramType = Dem.createNode(Node::Kind::Type);
paramType->addChild(totalInput, Dem);
parameters->addChild(paramType, Dem);
NodePointer resultTy = _swift_buildDemanglingForMetadata(func->ResultType,
Dem);
if (!resultTy)
return nullptr;
NodePointer result = Dem.createNode(Node::Kind::ReturnType);
result->addChild(resultTy, Dem);
auto funcNode = Dem.createNode(kind);
if (func->hasGlobalActor()) {
auto globalActorTypeNode =
_swift_buildDemanglingForMetadata(func->getGlobalActor(), Dem);
if (!globalActorTypeNode)
return nullptr;
NodePointer globalActorNode =
Dem.createNode(Node::Kind::GlobalActorFunctionType);
globalActorNode->addChild(globalActorTypeNode, Dem);
funcNode->addChild(globalActorNode, Dem);
}
switch (func->getDifferentiabilityKind().Value) {
case FunctionMetadataDifferentiabilityKind::NonDifferentiable:
break;
case FunctionMetadataDifferentiabilityKind::Forward:
funcNode->addChild(Dem.createNode(
Node::Kind::DifferentiableFunctionType,
(Node::IndexType)MangledDifferentiabilityKind::Forward), Dem);
break;
case FunctionMetadataDifferentiabilityKind::Reverse:
funcNode->addChild(Dem.createNode(
Node::Kind::DifferentiableFunctionType,
(Node::IndexType)MangledDifferentiabilityKind::Reverse), Dem);
break;
case FunctionMetadataDifferentiabilityKind::Normal:
funcNode->addChild(Dem.createNode(
Node::Kind::DifferentiableFunctionType,
(Node::IndexType)MangledDifferentiabilityKind::Normal), Dem);
break;
case FunctionMetadataDifferentiabilityKind::Linear:
funcNode->addChild(Dem.createNode(
Node::Kind::DifferentiableFunctionType,
(Node::IndexType)MangledDifferentiabilityKind::Linear), Dem);
break;
}
if (func->isThrowing()) {
if (auto thrownError = func->getThrownError()) {
auto thrownErrorTypeNode =
_swift_buildDemanglingForMetadata(thrownError, Dem);
if (!thrownErrorTypeNode)
return nullptr;
NodePointer thrownErrorNode =
Dem.createNode(Node::Kind::TypedThrowsAnnotation);
thrownErrorNode->addChild(thrownErrorTypeNode, Dem);
funcNode->addChild(thrownErrorNode, Dem);
} else {
funcNode->addChild(Dem.createNode(Node::Kind::ThrowsAnnotation), Dem);
}
}
if (func->isSendable()) {
funcNode->addChild(
Dem.createNode(Node::Kind::ConcurrentFunctionType), Dem);
}
if (func->isAsync())
funcNode->addChild(Dem.createNode(Node::Kind::AsyncAnnotation), Dem);
if (func->getExtendedFlags().hasSendingResult())
funcNode->addChild(Dem.createNode(Node::Kind::SendingResultFunctionType),
Dem);
funcNode->addChild(parameters, Dem);
funcNode->addChild(result, Dem);
return funcNode;
}
case MetadataKind::Metatype: {
auto metatype = static_cast<const MetatypeMetadata *>(type);
auto instance = _swift_buildDemanglingForMetadata(metatype->InstanceType,
Dem);
if (!instance)
return nullptr;
auto typeNode = Dem.createNode(Node::Kind::Type);
typeNode->addChild(instance, Dem);
auto node = Dem.createNode(Node::Kind::Metatype);
node->addChild(typeNode, Dem);
return node;
}
case MetadataKind::Tuple: {
auto tuple = static_cast<const TupleTypeMetadata *>(type);
const char *labels = tuple->Labels;
auto tupleNode = Dem.createNode(Node::Kind::Tuple);
for (unsigned i = 0, e = tuple->NumElements; i < e; ++i) {
auto elt = Dem.createNode(Node::Kind::TupleElement);
// Add a label child if applicable:
if (labels) {
// Look for the next space in the labels string.
if (const char *space = strchr(labels, ' ')) {
// If there is one, and the label isn't empty, add a label child.
if (labels != space) {
auto eltName =
Dem.createNode(Node::Kind::TupleElementName,
llvm::StringRef(labels, space - labels));
elt->addChild(eltName, Dem);
}
// Skip past the space.
labels = space + 1;
}
}
// Add the element type child.
auto eltType =
_swift_buildDemanglingForMetadata(tuple->getElement(i).Type, Dem);
if (!eltType)
return nullptr;
if (eltType->getKind() == Node::Kind::Type) {
elt->addChild(eltType, Dem);
} else {
auto type = Dem.createNode(Node::Kind::Type);
type->addChild(eltType, Dem);
elt->addChild(type, Dem);
}
// Add the completed element to the tuple.
tupleNode->addChild(elt, Dem);
}
return tupleNode;
}
case MetadataKind::HeapLocalVariable:
case MetadataKind::HeapGenericLocalVariable:
case MetadataKind::ErrorObject:
break;
case MetadataKind::Opaque:
default: {
if (auto builtinType = _buildDemanglerForBuiltinType(type, Dem))
return builtinType;
// FIXME: Some opaque types do have manglings, but we don't have enough info
// to figure them out.
break;
}
}
// Not a type.
return nullptr;
}
Demangle::NodePointer
swift::_buildDemanglingForGenericType(const TypeContextDescriptor *description,
const void *const *arguments,
Demangle::Demangler &Dem) {
auto kind = description->getKind();
if (kind != ContextDescriptorKind::Class &&
kind != ContextDescriptorKind::Enum &&
kind != ContextDescriptorKind::Struct)
return nullptr;
llvm::SmallVector<NodePointer, 8> demangledGenerics;
if (!_buildDemanglingForGenericArgs(description,
(const Metadata *const *)arguments,
demangledGenerics, Dem))
return nullptr;
return _buildDemanglingForContext(description, demangledGenerics, Dem);
}
// NB: This function is not used directly in the Swift codebase, but is
// exported for Xcode support and is used by the sanitizers. Please coordinate
// before changing.
//
/// Demangles a Swift symbol name.
///
/// \param mangledName is the symbol name that needs to be demangled.
/// \param mangledNameLength is the length of the string that should be
/// demangled.
/// \param outputBuffer is the user provided buffer where the demangled name
/// will be placed. If nullptr, a new buffer will be malloced. In that case,
/// the user of this API is responsible for freeing the returned buffer.
/// \param outputBufferSize is the size of the output buffer. If the demangled
/// name does not fit into the outputBuffer, the output will be truncated and
/// the size will be updated, indicating how large the buffer should be.
/// \param flags can be used to select the demangling style. TODO: We should
//// define what these will be.
/// \returns the demangled name. Returns nullptr if the input String is not a
/// Swift mangled name.
SWIFT_RUNTIME_EXPORT
char *swift_demangle(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
size_t *outputBufferSize,
uint32_t flags) {
if (flags != 0) {
swift::fatalError(0, "Only 'flags' value of '0' is currently supported.");
}
if (outputBuffer != nullptr && outputBufferSize == nullptr) {
swift::fatalError(0, "'outputBuffer' is passed but the size is 'nullptr'.");
}
// Check if we are dealing with Swift mangled name, otherwise, don't try
// to demangle and send indication to the user.
if (!Demangle::isSwiftSymbol(mangledName))
return nullptr; // Not a mangled name
#if !SWIFT_STDLIB_HAS_TYPE_PRINTING
return nullptr;
#else
// Demangle the name.
auto options = Demangle::DemangleOptions();
options.DisplayDebuggerGeneratedModule = false;
auto result =
Demangle::demangleSymbolAsString(mangledName,
mangledNameLength,
options);
// If the output buffer is not provided, malloc memory ourselves.
if (outputBuffer == nullptr || *outputBufferSize == 0) {
return strdup(result.c_str());
}
// Copy into the provided buffer.
_swift_strlcpy(outputBuffer, result.c_str(), *outputBufferSize);
// Indicate a failure if the result did not fit and was truncated
// by setting the required outputBufferSize.
if (*outputBufferSize < result.length() + 1) {
*outputBufferSize = result.length() + 1;
}
return outputBuffer;
#endif
}
|