Skip to content

Commit 584eda3

Browse files
committed
ClangImporter: More principled use of mapType{Into,OutOf}Context()
Before we would construct types containing a mix of interface and contextual types, and then map them in and out. Straighten this out. Note that I've also had to start untangling the issue where synthesized ParamDecls do not have an interface type.
1 parent c684ad2 commit 584eda3

File tree

7 files changed

+175
-179
lines changed

7 files changed

+175
-179
lines changed

include/swift/AST/ParameterList.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ class alignas(ParamDecl *) ParameterList final :
142142
/// null type if one of the ParamDecls does not have a type set for it yet.
143143
Type getType(const ASTContext &C) const;
144144

145+
/// Return a TupleType or ParenType for this parameter list written in terms
146+
/// of interface types.
147+
Type getInterfaceType(DeclContext *DC) const;
148+
145149
/// Return the full function type for a set of curried parameter lists that
146-
/// returns the specified result type. This returns a null type if one of the
147-
/// ParamDecls does not have a type set for it yet.
148-
///
149-
static Type getFullType(Type resultType, ArrayRef<ParameterList*> PL);
150+
/// returns the specified result type written in terms of interface types.
151+
static Type getFullInterfaceType(Type resultType, ArrayRef<ParameterList*> PL,
152+
DeclContext *DC);
150153

151154

152155
/// Return the full source range of this parameter.

lib/AST/Parameter.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,54 @@ Type ParameterList::getType(const ASTContext &C) const {
131131
return TupleType::get(argumentInfo, C);
132132
}
133133

134+
/// Hack to deal with the fact that Sema/CodeSynthesis.cpp creates ParamDecls
135+
/// containing contextual types.
136+
Type ParameterList::getInterfaceType(DeclContext *DC) const {
137+
auto &C = DC->getASTContext();
138+
139+
if (size() == 0)
140+
return TupleType::getEmpty(C);
141+
142+
SmallVector<TupleTypeElt, 8> argumentInfo;
143+
144+
for (auto P : *this) {
145+
assert(P->hasType());
146+
147+
Type type;
148+
if (P->hasInterfaceType())
149+
type = P->getInterfaceType();
150+
else if (!P->getTypeLoc().hasLocation())
151+
type = ArchetypeBuilder::mapTypeOutOfContext(DC, P->getType());
152+
else
153+
type = P->getType();
154+
assert(!type->hasArchetype());
155+
156+
argumentInfo.push_back({
157+
type, P->getArgumentName(),
158+
P->getDefaultArgumentKind(), P->isVariadic()
159+
});
160+
}
161+
162+
return TupleType::get(argumentInfo, C);
163+
}
164+
134165

135166
/// Return the full function type for a set of curried parameter lists that
136167
/// returns the specified result type. This returns a null type if one of the
137168
/// ParamDecls does not have a type set for it yet.
138169
///
139-
Type ParameterList::getFullType(Type resultType, ArrayRef<ParameterList*> PLL) {
170+
Type ParameterList::getFullInterfaceType(Type resultType,
171+
ArrayRef<ParameterList*> PLL,
172+
DeclContext *DC) {
140173
auto result = resultType;
141-
auto &C = result->getASTContext();
142-
143174
for (auto PL : reversed(PLL)) {
144-
auto paramType = PL->getType(C);
145-
if (!paramType) return Type();
175+
auto paramType = PL->getInterfaceType(DC);
146176
result = FunctionType::get(paramType, result);
147177
}
148178
return result;
149179
}
150180

181+
151182
/// Return the full source range of this parameter list.
152183
SourceRange ParameterList::getSourceRange() const {
153184
// If we have locations for the parens, then they define our range.

0 commit comments

Comments
 (0)