Function return type checking
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
namespace Solstice {
|
||||
enum class BaseType {
|
||||
@@ -137,8 +139,9 @@ namespace Solstice {
|
||||
};
|
||||
|
||||
struct Function {
|
||||
PossibleType returnTypes;
|
||||
std::vector<PossibleType> argumentTypes;
|
||||
std::unordered_map<std::vector<Type>, Type> returnTypes;
|
||||
std::vector<std::vector<Type>> invalidCombinations;
|
||||
Function() = default;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "typechecker.hpp"
|
||||
#include "type.hpp"
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
@@ -153,6 +154,45 @@ namespace Solstice {
|
||||
variables[*id].unknownType = true;
|
||||
}
|
||||
}
|
||||
static inline std::vector<std::vector<Type>> doCartesianProductOnTypeSets(const std::vector<PossibleType>& typeSets) {
|
||||
if (typeSets.empty()) return {};
|
||||
for (const auto& s : typeSets) {
|
||||
if (s.possiblities.empty()) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<Type>> result;
|
||||
std::vector<std::unordered_set<Type>::const_iterator> iterators;
|
||||
|
||||
// Initialize iterators to the start of each set
|
||||
for (const auto& s : typeSets) {
|
||||
iterators.push_back(s.possiblities.begin());
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// Collect current elements from each set
|
||||
std::vector<Type> current;
|
||||
for (const auto& it : iterators) {
|
||||
current.push_back(*it);
|
||||
}
|
||||
result.push_back(current);
|
||||
|
||||
// Increment like an odometer (rightmost digit first)
|
||||
int idx = iterators.size() - 1;
|
||||
while (idx >= 0) {
|
||||
iterators[idx]++;
|
||||
if (iterators[idx] != typeSets[idx].possiblities.end()) break;
|
||||
iterators[idx] = typeSets[idx].possiblities.begin();
|
||||
idx--;
|
||||
}
|
||||
|
||||
// If we wrapped the leftmost position, we're done
|
||||
if (idx < 0) break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void TypeChecker::checkFunctionBindNodeType(Node& node) {
|
||||
auto id = node.children[0].getIdentifier();
|
||||
@@ -205,13 +245,38 @@ namespace Solstice {
|
||||
|
||||
typeChecker.checkTypes();
|
||||
|
||||
// collect the (possibly still-polymorphic) narrowed argument types,
|
||||
// and the inferred return type from the body's implicit return value
|
||||
// collect the (possibly still-polymorphic) narrowed argument types
|
||||
Function fn;
|
||||
for (const auto& name : paramNames) {
|
||||
fn.argumentTypes.push_back(typeChecker.variables[name]);
|
||||
}
|
||||
fn.returnTypes = node.children[2].ptype;
|
||||
|
||||
std::vector<std::vector<Type>> candidates = doCartesianProductOnTypeSets(fn.argumentTypes);
|
||||
|
||||
// re-check the body once per candidate with every parameter bound to
|
||||
// a single concrete type. concrete (non-unknownType) variables always
|
||||
// take the Narrowing::None path, so this reuses all the existing
|
||||
// operator-checking logic and naturally rejects combinations that
|
||||
// only looked valid in the marginal sets.
|
||||
for (const auto& combo : candidates) {
|
||||
TypeChecker concreteChecker(*this);
|
||||
concreteChecker.setNode(node.children[2]);
|
||||
concreteChecker.inFunction = true;
|
||||
for (std::size_t i = 0; i < paramNames.size(); i++) {
|
||||
concreteChecker.setVariable(paramNames[i], combo[i]);
|
||||
}
|
||||
try {
|
||||
concreteChecker.checkTypes();
|
||||
auto retType = node.children[2].ptype.getOnlyType();
|
||||
if (retType.has_value()) {
|
||||
fn.returnTypes[combo] = *retType;
|
||||
} else {
|
||||
fn.invalidCombinations.push_back(combo);
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
fn.invalidCombinations.push_back(combo);
|
||||
}
|
||||
}
|
||||
|
||||
functions[*id] = fn;
|
||||
}
|
||||
@@ -245,6 +310,13 @@ namespace Solstice {
|
||||
auto& left = node.children[0];
|
||||
auto& right = node.children[1];
|
||||
|
||||
// this node may be getting checked again (e.g. once polymorphically,
|
||||
// then once more per concrete argument combination in
|
||||
// checkFunctionBindNodeType) - possiblities is only ever inserted
|
||||
// into below, so it must be reset here or results from a previous
|
||||
// pass over this same node would leak into this one
|
||||
node.ptype.possiblities.clear();
|
||||
|
||||
// narrow types if we need to
|
||||
switch (doesNodeChildrenNeedNarrowing(node)) {
|
||||
case Narrowing::Both: {
|
||||
@@ -323,6 +395,10 @@ namespace Solstice {
|
||||
|
||||
}
|
||||
|
||||
void TypeChecker::narrowFunctionCallNode(Node& node) {
|
||||
|
||||
}
|
||||
|
||||
void TypeChecker::checkSetNodeType(Node& node) {
|
||||
auto id = node.children[0].getIdentifier();
|
||||
if (!id.has_value()) {
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace Solstice {
|
||||
|
||||
Narrowing doesNodeChildrenNeedNarrowing(Node& node);
|
||||
void narrowBinaryNode(Node& node, std::unordered_map<TypePair, Type>& overloads);
|
||||
void narrowFunctionCallNode(Node& node);
|
||||
|
||||
void checkAddType(Node& node);
|
||||
void checkSubtractType(Node& node);
|
||||
|
||||
Reference in New Issue
Block a user