17
17
#ifndef SWIFT_REMOTE_METADATAREADER_H
18
18
#define SWIFT_REMOTE_METADATAREADER_H
19
19
20
+ #include " swift/AST/Types.h"
20
21
#include " swift/Runtime/Metadata.h"
21
22
#include " swift/Remote/MemoryReader.h"
22
23
#include " swift/Demangling/Demangler.h"
29
30
namespace swift {
30
31
namespace remote {
31
32
33
+ template <typename BuiltType> class FunctionParam {
34
+ StringRef Label;
35
+ BuiltType Type;
36
+ ParameterTypeFlags Flags;
37
+
38
+ FunctionParam (StringRef label, BuiltType type, ParameterTypeFlags flags)
39
+ : Label(label), Type(type), Flags(flags) {}
40
+
41
+ public:
42
+ explicit FunctionParam () {}
43
+
44
+ FunctionParam (BuiltType type) : Type(type) {}
45
+
46
+ StringRef getLabel () const { return Label; }
47
+ BuiltType getType () const { return Type; }
48
+ ParameterTypeFlags getFlags () const { return Flags; }
49
+
50
+ void setLabel (StringRef label) { Label = label; }
51
+ void setType (BuiltType type) { Type = type; }
52
+
53
+ void setVariadic () { Flags = Flags.withVariadic (true ); }
54
+ void setShared () { Flags = Flags.withShared (true ); }
55
+ void setInOut () { Flags = Flags.withInOut (true ); }
56
+
57
+ FunctionParam withLabel (StringRef label) const {
58
+ return FunctionParam (label, Type, Flags);
59
+ }
60
+
61
+ FunctionParam withType (BuiltType type) const {
62
+ return FunctionParam (Label, type, Flags);
63
+ }
64
+
65
+ FunctionParam withFlags (ParameterTypeFlags flags) const {
66
+ return FunctionParam (Label, Type, flags);
67
+ }
68
+ };
69
+
32
70
// / A utility class for constructing abstract types from
33
71
// / a textual mangling.
34
72
template <typename BuilderType>
@@ -219,16 +257,14 @@ class TypeDecoder {
219
257
Node->getChild (0 )->getKind () == NodeKind::ThrowsAnnotation;
220
258
flags = flags.withThrows (true );
221
259
222
- std::vector<BuiltType> arguments;
223
- std::vector<bool > argsAreInOut;
260
+ std::vector<FunctionParam<BuiltType>> parameters;
224
261
if (!decodeMangledFunctionInputType (Node->getChild (isThrow ? 1 : 0 ),
225
- arguments, argsAreInOut, flags ))
262
+ parameters ))
226
263
return BuiltType ();
227
264
228
265
auto result = decodeMangledType (Node->getChild (isThrow ? 2 : 1 ));
229
266
if (!result) return BuiltType ();
230
- return Builder.createFunctionType (arguments, argsAreInOut,
231
- result, flags);
267
+ return Builder.createFunctionType (parameters, result, flags);
232
268
}
233
269
case NodeKind::ImplFunctionType: {
234
270
// Minimal support for lowered function types. These come up in
@@ -263,15 +299,13 @@ class TypeDecoder {
263
299
}
264
300
265
301
// Completely punt on argument types and results.
266
- std::vector<BuiltType> arguments;
267
- std::vector<bool > argsAreInOut;
302
+ std::vector<FunctionParam<BuiltType>> parameters;
268
303
269
304
std::vector<BuiltType> elements;
270
305
std::string labels;
271
306
auto result = Builder.createTupleType (elements, std::move (labels), false );
272
307
273
- return Builder.createFunctionType (arguments, argsAreInOut,
274
- result, flags);
308
+ return Builder.createFunctionType (parameters, result, flags);
275
309
}
276
310
case NodeKind::ArgumentTuple:
277
311
return decodeMangledType (Node->getChild (0 ));
@@ -397,55 +431,92 @@ class TypeDecoder {
397
431
return true ;
398
432
}
399
433
400
- bool decodeMangledFunctionInputType (const Demangle::NodePointer &node,
401
- std::vector<BuiltType> &args,
402
- std::vector<bool > &argsAreInOut,
403
- FunctionTypeFlags &flags) {
434
+ bool decodeMangledFunctionInputType (
435
+ const Demangle::NodePointer &node,
436
+ std::vector<FunctionParam<BuiltType>> ¶ms) {
404
437
// Look through a couple of sugar nodes.
405
438
if (node->getKind () == NodeKind::Type ||
406
439
node->getKind () == NodeKind::ArgumentTuple) {
407
- return decodeMangledFunctionInputType (node->getFirstChild (),
408
- args, argsAreInOut, flags);
440
+ return decodeMangledFunctionInputType (node->getFirstChild (), params);
409
441
}
410
442
411
- auto decodeSingleHelper =
412
- [&](const Demangle::NodePointer &typeNode, bool argIsInOut) -> bool {
413
- BuiltType argType = decodeMangledType (typeNode);
414
- if (!argType) return false ;
443
+ auto decodeParamTypeAndFlags =
444
+ [&](const Demangle::NodePointer &typeNode,
445
+ FunctionParam<BuiltType> ¶m) -> bool {
446
+ Demangle::NodePointer node = typeNode;
447
+ switch (node->getKind ()) {
448
+ case NodeKind::InOut:
449
+ param.setInOut ();
450
+ node = node->getFirstChild ();
451
+ break ;
452
+
453
+ case NodeKind::Shared:
454
+ param.setShared ();
455
+ node = node->getFirstChild ();
456
+ break ;
457
+
458
+ default :
459
+ break ;
460
+ }
461
+
462
+ auto paramType = decodeMangledType (node);
463
+ if (!paramType)
464
+ return false ;
415
465
416
- args.push_back (argType);
417
- argsAreInOut.push_back (argIsInOut);
466
+ param.setType (paramType);
418
467
return true ;
419
468
};
420
- auto decodeSingle =
421
- [&](const Demangle::NodePointer &typeNode) -> bool {
422
- if (typeNode->getKind () == NodeKind::InOut) {
423
- return decodeSingleHelper (typeNode->getFirstChild (), true );
424
- } else {
425
- return decodeSingleHelper (typeNode, false );
469
+
470
+ auto decodeParam = [&](const Demangle::NodePointer ¶mNode)
471
+ -> Optional<FunctionParam<BuiltType>> {
472
+ if (paramNode->getKind () != NodeKind::TupleElement)
473
+ return None;
474
+
475
+ FunctionParam<BuiltType> param;
476
+ for (const auto &child : *paramNode) {
477
+ switch (child->getKind ()) {
478
+ case NodeKind::TupleElementName:
479
+ param.setLabel (child->getText ());
480
+ break ;
481
+
482
+ case NodeKind::VariadicMarker:
483
+ param.setVariadic ();
484
+ break ;
485
+
486
+ case NodeKind::Type:
487
+ if (!decodeParamTypeAndFlags (child->getFirstChild (), param))
488
+ return None;
489
+ break ;
490
+
491
+ default :
492
+ return None;
493
+ }
426
494
}
495
+
496
+ return param;
427
497
};
428
498
429
499
// Expand a single level of tuple.
430
500
if (node->getKind () == NodeKind::Tuple) {
431
- // TODO: preserve variadic somewhere?
432
-
433
501
// Decode all the elements as separate arguments.
434
502
for (const auto &elt : *node) {
435
- if (elt->getKind () != NodeKind::TupleElement)
436
- return false ;
437
- auto typeNode = elt->getChild (elt->getNumChildren () - 1 );
438
- if (typeNode->getKind () != NodeKind::Type)
439
- return false ;
440
- if (!decodeSingle (typeNode->getFirstChild ()))
503
+ auto param = decodeParam (elt);
504
+ if (!param)
441
505
return false ;
506
+
507
+ params.push_back (std::move (*param));
442
508
}
443
509
444
510
return true ;
445
511
}
446
512
447
513
// Otherwise, handle the type as a single argument.
448
- return decodeSingle (node);
514
+ FunctionParam<BuiltType> param;
515
+ if (!decodeParamTypeAndFlags (node, param))
516
+ return false ;
517
+
518
+ params.push_back (std::move (param));
519
+ return true ;
449
520
}
450
521
};
451
522
@@ -734,8 +805,7 @@ class MetadataReader {
734
805
case MetadataKind::Function: {
735
806
auto Function = cast<TargetFunctionTypeMetadata<Runtime>>(Meta);
736
807
737
- std::vector<BuiltType> Arguments;
738
- std::vector<bool > ArgumentIsInOut;
808
+ std::vector<FunctionParam<BuiltType>> Parameters;
739
809
StoredPointer ArgumentAddress = MetadataAddress +
740
810
sizeof (TargetFunctionTypeMetadata<Runtime>);
741
811
for (StoredPointer i = 0 ; i < Function->getNumArguments (); ++i,
@@ -745,15 +815,21 @@ class MetadataReader {
745
815
&FlaggedArgumentAddress))
746
816
return BuiltType ();
747
817
818
+ FunctionParam<BuiltType> Param;
819
+
748
820
// TODO: Use target-agnostic FlaggedPointer to mask this!
749
821
const auto InOutMask = (StoredPointer) 1 ;
750
- ArgumentIsInOut.push_back ((FlaggedArgumentAddress & InOutMask) != 0 );
751
- FlaggedArgumentAddress &= ~InOutMask;
822
+ // FIXME: Add import parameter related flags from metadata
823
+ if ((FlaggedArgumentAddress & InOutMask) != 0 )
824
+ Param.setInOut ();
752
825
753
- if (auto ArgumentTypeRef = readTypeFromMetadata (FlaggedArgumentAddress))
754
- Arguments.push_back (ArgumentTypeRef);
755
- else
826
+ FlaggedArgumentAddress &= ~InOutMask;
827
+ if (auto ParamTypeRef = readTypeFromMetadata (FlaggedArgumentAddress)) {
828
+ Param.setType (ParamTypeRef);
829
+ Parameters.push_back (std::move (Param));
830
+ } else {
756
831
return BuiltType ();
832
+ }
757
833
}
758
834
759
835
auto Result = readTypeFromMetadata (Function->ResultType );
@@ -762,9 +838,8 @@ class MetadataReader {
762
838
763
839
auto flags = FunctionTypeFlags ().withConvention (Function->getConvention ())
764
840
.withThrows (Function->throws ());
765
- auto BuiltFunction = Builder.createFunctionType (Arguments,
766
- ArgumentIsInOut,
767
- Result, flags);
841
+ auto BuiltFunction =
842
+ Builder.createFunctionType (Parameters, Result, flags);
768
843
TypeCache[MetadataAddress] = BuiltFunction;
769
844
return BuiltFunction;
770
845
}
0 commit comments