Fixed up vector and make template searching functions use std::set to remove false positives on multiple matching templates

This commit is contained in:
Nathan Braswell
2015-01-07 03:03:14 -05:00
parent b66e8cbaa5
commit ad0e90f74a
2 changed files with 11 additions and 11 deletions

View File

@@ -873,7 +873,7 @@ NodeTree<ASTData>* ASTTransformation::functionLookup(NodeTree<ASTData>* scope, s
//Lookup class templates. It evaluates possible matches on traits //Lookup class templates. It evaluates possible matches on traits
NodeTree<ASTData>* ASTTransformation::templateClassLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type*> templateInstantiationTypes) { NodeTree<ASTData>* ASTTransformation::templateClassLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type*> templateInstantiationTypes) {
std::vector<NodeTree<ASTData>*> mostFittingTemplates; std::set<NodeTree<ASTData>*> mostFittingTemplates;
int bestNumTraitsSatisfied = -1; int bestNumTraitsSatisfied = -1;
auto possibleMatches = scopeLookup(scope, lookup); auto possibleMatches = scopeLookup(scope, lookup);
std::cout << "Template Class instantiation has " << possibleMatches.size() << " possible matches." << std::endl; std::cout << "Template Class instantiation has " << possibleMatches.size() << " possible matches." << std::endl;
@@ -919,7 +919,7 @@ NodeTree<ASTData>* ASTTransformation::templateClassLookup(NodeTree<ASTData>* sco
bestNumTraitsSatisfied = currentTraitsSatisfied; bestNumTraitsSatisfied = currentTraitsSatisfied;
} else if (currentTraitsSatisfied < bestNumTraitsSatisfied) } else if (currentTraitsSatisfied < bestNumTraitsSatisfied)
continue; continue;
mostFittingTemplates.push_back(i); mostFittingTemplates.insert(i);
std::cout << "Current class fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl; std::cout << "Current class fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl;
} }
if (!mostFittingTemplates.size()) { if (!mostFittingTemplates.size()) {
@@ -929,12 +929,12 @@ NodeTree<ASTData>* ASTTransformation::templateClassLookup(NodeTree<ASTData>* sco
std::cout << "Multiple template classes fit with equal number of traits satisfied for " << lookup << "!" << std::endl; std::cout << "Multiple template classes fit with equal number of traits satisfied for " << lookup << "!" << std::endl;
throw "Multiple matching template classes"; throw "Multiple matching template classes";
} }
return mostFittingTemplates[0]; return *mostFittingTemplates.begin();
} }
//Lookup function for template functions. It has some extra concerns compared to function lookup, namely traits //Lookup function for template functions. It has some extra concerns compared to function lookup, namely traits
NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type*> templateInstantiationTypes, std::vector<Type> types) { NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type*> templateInstantiationTypes, std::vector<Type> types) {
std::vector<NodeTree<ASTData>*> mostFittingTemplates; std::set<NodeTree<ASTData>*> mostFittingTemplates;
int bestNumTraitsSatisfied = -1; int bestNumTraitsSatisfied = -1;
auto possibleMatches = scopeLookup(scope, lookup); auto possibleMatches = scopeLookup(scope, lookup);
std::cout << "Template Function instantiation has " << possibleMatches.size() << " possible matches." << std::endl; std::cout << "Template Function instantiation has " << possibleMatches.size() << " possible matches." << std::endl;
@@ -1004,7 +1004,7 @@ NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>*
bestNumTraitsSatisfied = currentTraitsSatisfied; bestNumTraitsSatisfied = currentTraitsSatisfied;
} else if (currentTraitsSatisfied < bestNumTraitsSatisfied) } else if (currentTraitsSatisfied < bestNumTraitsSatisfied)
continue; continue;
mostFittingTemplates.push_back(i); mostFittingTemplates.insert(i);
std::cout << "Current function fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl; std::cout << "Current function fits, satisfying " << currentTraitsSatisfied << " traits" << std::endl;
} }
if (!mostFittingTemplates.size()) { if (!mostFittingTemplates.size()) {
@@ -1014,7 +1014,7 @@ NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>*
std::cout << "Multiple template functions fit with equal number of traits satisfied for " << lookup << "!" << std::endl; std::cout << "Multiple template functions fit with equal number of traits satisfied for " << lookup << "!" << std::endl;
throw "Multiple matching template functions"; throw "Multiple matching template functions";
} }
return mostFittingTemplates[0]; return *mostFittingTemplates.begin();
} }
//Extract pairs of type names and traits //Extract pairs of type names and traits

View File

@@ -1,6 +1,6 @@
import mem; import mem:*;
import util; import util:*;
import io; import io:*;
typedef template<T> vector (Destructable) { typedef template<T> vector (Destructable) {
|T*| data; |T*| data;
@@ -27,11 +27,11 @@ typedef template<T> vector (Destructable) {
delete<T>(data, 0); delete<T>(data, 0);
return true; return true;
} }
|T| at(|int| index) { |T| at(|int| index) {
return get(index); return get(index);
} }
|T| get(|int| index) { |T| get(|int| index) {
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
println("Vector access out of bounds! Retuning 0th element as sanest option"); println("Vector access out of bounds! Retuning 0th element as sanest option");