Skip to content

Commit d9c2fac

Browse files
committed
Reflection: Add function representation to FunctionTypeRef
1 parent 8cb268d commit d9c2fac

File tree

7 files changed

+49
-8
lines changed

7 files changed

+49
-8
lines changed

include/swift/Reflection/TypeRef.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "llvm/ADT/DenseMap.h"
2222
#include "llvm/Support/Casting.h"
23+
#include "swift/ABI/MetadataValues.h"
2324

2425
#include <iostream>
2526

@@ -187,16 +188,20 @@ class TupleTypeRef final : public TypeRef {
187188
class FunctionTypeRef final : public TypeRef {
188189
std::vector<const TypeRef *> Arguments;
189190
const TypeRef *Result;
191+
FunctionTypeFlags Flags;
190192

191193
public:
192-
FunctionTypeRef(std::vector<const TypeRef *> Arguments, const TypeRef *Result)
193-
: TypeRef(TypeRefKind::Function), Arguments(Arguments), Result(Result) {}
194+
FunctionTypeRef(std::vector<const TypeRef *> Arguments, const TypeRef *Result,
195+
FunctionTypeFlags Flags)
196+
: TypeRef(TypeRefKind::Function), Arguments(Arguments), Result(Result),
197+
Flags(Flags) {}
194198

195199
template <typename Allocator>
196200
static FunctionTypeRef *create(Allocator &A,
197201
std::vector<const TypeRef *> Arguments,
198-
const TypeRef *Result) {
199-
return A.template makeTypeRef<FunctionTypeRef>(Arguments, Result);
202+
const TypeRef *Result,
203+
FunctionTypeFlags Flags) {
204+
return A.template makeTypeRef<FunctionTypeRef>(Arguments, Result, Flags);
200205
}
201206

202207
const std::vector<const TypeRef *> &getArguments() const {
@@ -207,6 +212,10 @@ class FunctionTypeRef final : public TypeRef {
207212
return Result;
208213
}
209214

215+
FunctionTypeFlags getFlags() const {
216+
return Flags;
217+
}
218+
210219
static bool classof(const TypeRef *TR) {
211220
return TR->getKind() == TypeRefKind::Function;
212221
}

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ class TypeRefBuilder {
148148
const TypeRef *result,
149149
FunctionTypeFlags flags) {
150150
// FIXME: don't ignore inOutArgs
151-
// FIXME: don't ignore flags
152-
return FunctionTypeRef::create(*this, args, result);
151+
return FunctionTypeRef::create(*this, args, result, flags);
153152
}
154153

155154
const ProtocolTypeRef *createProtocolType(const std::string &moduleName,

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,25 @@ class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
109109

110110
void visitFunctionTypeRef(const FunctionTypeRef *F) {
111111
printHeader("function");
112+
113+
switch (F->getFlags().getConvention()) {
114+
case FunctionMetadataConvention::Swift:
115+
break;
116+
case FunctionMetadataConvention::Block:
117+
printField("convention", "block");
118+
break;
119+
case FunctionMetadataConvention::Thin:
120+
printField("convention", "thin");
121+
break;
122+
case FunctionMetadataConvention::CFunctionPointer:
123+
printField("convention", "c");
124+
break;
125+
}
126+
112127
for (auto Arg : F->getArguments())
113128
printRec(Arg);
114129
printRec(F->getResult());
130+
115131
OS << ')';
116132
}
117133

@@ -455,7 +471,7 @@ class TypeRefSubstitution
455471
auto SubstitutedResult = visit(F->getResult());
456472

457473
return FunctionTypeRef::create(Builder, SubstitutedArguments,
458-
SubstitutedResult);
474+
SubstitutedResult, F->getFlags());
459475
}
460476

461477
const TypeRef *visitProtocolTypeRef(const ProtocolTypeRef *P) {

test/Reflection/Inputs/ConcreteTypes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public struct S {
3333
public let aTuple: (C, Box<S>, Box<E>, Int)
3434
public let aMetatype: C.Type
3535
public let aFunction: (C, S, E, Int) -> (Int)
36+
public let aFunctionWithThinRepresentation: @convention(thin) () -> ()
37+
public let aFunctionWithCRepresentation: @convention(c) () -> ()
3638

3739
public struct NestedS {
3840
public let aField: Int

test/Reflection/Inputs/ObjectiveCTypes.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ public class OC : NSObject {
44
public let nsObject: NSObject
55
public let nsString: NSString
66
public let cfString: CFString
7-
public init(nsObject: NSObject, nsString: NSString, cfString: CFString) {
7+
public let aBlock: @convention(block) () -> ()
8+
public init(nsObject: NSObject, nsString: NSString, cfString: CFString,
9+
aBlock: @convention(block) () -> ()) {
810
self.nsObject = nsObject
911
self.nsString = nsString
1012
self.cfString = cfString
13+
self.aBlock = aBlock
1114
}
1215
}
1316

test/Reflection/typeref_decoding.result.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ aFunction: (TypesToReflect.C, TypesToReflect.S, TypesToReflect.E, Swift.Int) ->
8787
(struct Swift.Int)
8888
(struct Swift.Int))
8989

90+
aFunctionWithThinRepresentation: @convention(thin) () -> ()
91+
(function convention=thin
92+
(tuple))
93+
94+
aFunctionWithCRepresentation: @convention(c) () -> ()
95+
(function convention=c
96+
(tuple))
97+
9098
TypesToReflect.E
9199
----------------
92100
Class: TypesToReflect.C

test/Reflection/typeref_decoding_objc.result.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ nsString: __ObjC.NSString
1111
cfString: __ObjC.CFString
1212
(class __ObjC.CFString)
1313

14+
aBlock: @convention(block) () -> ()
15+
(function convention=block
16+
(tuple))
17+
1418
TypesToReflect.GenericOC
1519
------------------------
1620
ocnss: TypesToReflect.GenericOC<__ObjC.NSString>

0 commit comments

Comments
 (0)