AST: Use AbstractFunctionDecl::computeType()

This commit is contained in:
Slava Pestov
2018-07-21 05:16:30 -07:00
parent f6b96da3e2
commit 106878005e
2 changed files with 26 additions and 76 deletions

View File

@@ -148,13 +148,6 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
FunctionType::ExtInfo Info = FunctionType::ExtInfo()) {
auto &Context = ResType->getASTContext();
SmallVector<TupleTypeElt, 4> tupleElts;
for (Type argType : argTypes)
tupleElts.push_back(argType);
Type ArgType = TupleType::get(tupleElts, Context);
Type FnType = FunctionType::get(ArgType, ResType, Info);
ModuleDecl *M = Context.TheBuiltinModule;
DeclContext *DC = &M->getMainFile(FileUnitKind::Builtin);
@@ -181,7 +174,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
/*SelfDecl=*/nullptr,
paramList,
TypeLoc::withoutLoc(ResType), DC);
FD->setInterfaceType(FnType);
FD->computeType(Info);
FD->setValidationToChecked();
FD->setImplicit();
FD->setAccess(AccessLevel::Public);
@@ -192,34 +185,18 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
static FuncDecl *
getBuiltinGenericFunction(Identifier Id,
ArrayRef<TupleTypeElt> ArgParamTypes,
ArrayRef<Type> ArgBodyTypes,
Type ResType,
Type ResBodyType,
GenericParamList *GenericParams,
GenericEnvironment *Env) {
assert(GenericParams && "Missing generic parameters");
auto &Context = ResType->getASTContext();
Type ArgParamType = TupleType::get(ArgParamTypes, Context);
// Compute the interface type.
SmallVector<GenericTypeParamType *, 1> GenericParamTypes;
for (auto gp : *GenericParams) {
GenericParamTypes.push_back(gp->getDeclaredInterfaceType()
->castTo<GenericTypeParamType>());
}
GenericSignature *Sig = Env->getGenericSignature();
Type InterfaceType = GenericFunctionType::get(Sig, ArgParamType, ResType,
AnyFunctionType::ExtInfo());
ModuleDecl *M = Context.TheBuiltinModule;
DeclContext *DC = &M->getMainFile(FileUnitKind::Builtin);
SmallVector<ParamDecl*, 4> params;
for (unsigned i = 0, e = ArgParamTypes.size(); i < e; i++) {
auto paramType = ArgBodyTypes[i];
auto paramIfaceType = ArgParamTypes[i].getType();
auto paramIfaceType = ArgParamTypes[i].getRawType();
auto specifier = (ArgParamTypes[i].getParameterFlags().isInOut())
? VarDecl::Specifier::InOut
: VarDecl::Specifier::Default;
@@ -227,14 +204,13 @@ getBuiltinGenericFunction(Identifier Id,
SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(),
Identifier(),
paramType->getInOutObjectType(), DC);
PD->setInterfaceType(paramIfaceType->getInOutObjectType());
Type(), DC);
PD->setInterfaceType(paramIfaceType);
PD->setValidationToChecked();
PD->setImplicit();
params.push_back(PD);
}
auto *paramList = ParameterList::create(Context, params);
DeclName Name(Context, Id, paramList);
@@ -246,11 +222,11 @@ getBuiltinGenericFunction(Identifier Id,
GenericParams,
/*SelfDecl=*/nullptr,
paramList,
TypeLoc::withoutLoc(ResBodyType), DC);
TypeLoc::withoutLoc(ResType), DC);
func->setInterfaceType(InterfaceType);
func->setValidationToChecked();
func->setGenericEnvironment(Env);
func->computeType();
func->setValidationToChecked();
func->setImplicit();
func->setAccess(AccessLevel::Public);
@@ -480,14 +456,9 @@ namespace {
private:
GenericParamList *TheGenericParamList;
SmallVector<GenericTypeParamDecl*, 2> GenericTypeParams;
GenericEnvironment *GenericEnv = nullptr;
SmallVector<TupleTypeElt, 4> InterfaceParams;
SmallVector<Type, 4> BodyParams;
Type InterfaceResult;
Type BodyResult;
public:
BuiltinGenericSignatureBuilder(ASTContext &ctx, unsigned numGenericParams = 1)
@@ -507,30 +478,27 @@ namespace {
template <class G>
void addParameter(const G &generator) {
Type gTyIface = generator.build(*this, false);
Type gTyIface = generator.build(*this);
InterfaceParams.push_back({gTyIface->getInOutObjectType(),
Identifier(), ParameterTypeFlags()});
BodyParams.push_back(generator.build(*this, true));
}
template <class G>
void addInOutParameter(const G &generator) {
Type gTyIface = generator.build(*this, false);
Type gTyIface = generator.build(*this);
auto iFaceflags = ParameterTypeFlags().withInOut(true);
InterfaceParams.push_back(TupleTypeElt(gTyIface->getInOutObjectType(),
Identifier(), iFaceflags));
BodyParams.push_back(generator.build(*this, true));
}
template <class G>
void setResult(const G &generator) {
InterfaceResult = generator.build(*this, false);
BodyResult = generator.build(*this, true);
InterfaceResult = generator.build(*this);
}
ValueDecl *build(Identifier name) {
return getBuiltinGenericFunction(name, InterfaceParams, BodyParams,
InterfaceResult, BodyResult,
return getBuiltinGenericFunction(name, InterfaceParams,
InterfaceResult,
TheGenericParamList,
GenericEnv);
}
@@ -540,27 +508,20 @@ namespace {
struct ConcreteGenerator {
Type TheType;
Type build(BuiltinGenericSignatureBuilder &builder, bool forBody) const {
Type build(BuiltinGenericSignatureBuilder &builder) const {
return TheType;
}
};
struct ParameterGenerator {
unsigned Index;
Type build(BuiltinGenericSignatureBuilder &builder, bool forBody) const {
auto gpType =
builder.GenericTypeParams[Index]->getDeclaredInterfaceType();
if (forBody) {
return builder.GenericEnv->mapTypeIntoContext(
gpType->castTo<GenericTypeParamType>());
}
return gpType;
Type build(BuiltinGenericSignatureBuilder &builder) const {
return builder.GenericTypeParams[Index]->getDeclaredInterfaceType();
}
};
struct LambdaGenerator {
std::function<Type(BuiltinGenericSignatureBuilder &,bool)> TheFunction;
Type build(BuiltinGenericSignatureBuilder &builder, bool forBody) const {
return TheFunction(builder, forBody);
std::function<Type(BuiltinGenericSignatureBuilder &)> TheFunction;
Type build(BuiltinGenericSignatureBuilder &builder) const {
return TheFunction(builder);
}
};
template <class T, class U>
@@ -568,9 +529,9 @@ namespace {
T Arg;
U Result;
FunctionType::ExtInfo ExtInfo;
Type build(BuiltinGenericSignatureBuilder &builder, bool forBody) const {
return FunctionType::get(Arg.build(builder, forBody),
Result.build(builder, forBody),
Type build(BuiltinGenericSignatureBuilder &builder) const {
return FunctionType::get(Arg.build(builder),
Result.build(builder),
ExtInfo);
}
};
@@ -578,8 +539,8 @@ namespace {
struct MetatypeGenerator {
T Object;
Optional<MetatypeRepresentation> Repr;
Type build(BuiltinGenericSignatureBuilder &builder, bool forBody) const {
return MetatypeType::get(Object.build(builder, forBody), Repr);
Type build(BuiltinGenericSignatureBuilder &builder) const {
return MetatypeType::get(Object.build(builder), Repr);
}
};
};
@@ -599,9 +560,9 @@ template <class... Gs>
static BuiltinGenericSignatureBuilder::LambdaGenerator
makeTuple(const Gs & ...elementGenerators) {
return {
[=](BuiltinGenericSignatureBuilder &builder, bool forBody) -> Type {
[=](BuiltinGenericSignatureBuilder &builder) -> Type {
TupleTypeElt elts[] = {
elementGenerators.build(builder, forBody)...
elementGenerators.build(builder)...
};
return TupleType::get(elts, builder.Context);
}