More all-rounded implementation of the new objecty features, several bugfixes, and updates to the standard library to behave. Vector still needs a bit more work for some operations

This commit is contained in:
Nathan Braswell
2015-06-05 00:34:24 -04:00
parent 6f9ceaa717
commit 7abab02fbf
15 changed files with 185 additions and 87 deletions

View File

@@ -1048,7 +1048,7 @@ void ASTTransformation::unifyType(NodeTree<Symbol> *syntaxType, Type type, std::
}
if (children.size() == 1) {
(*templateTypeMap)[concatSymbolTree(children.back())] = type;
(*templateTypeMap)[concatSymbolTree(children.front())] = type;
// I also kina feel like maybe I need to worry about typeMap, which is why
// I passed it in... It would contain the typemap from our scope if we are
// doing a template member function of a templated object
@@ -1093,8 +1093,9 @@ void ASTTransformation::unifyTemplateFunction(NodeTree<ASTData>* templateFunctio
std::map<std::string, Type> templateTypeMap;
for (int i = 0; i < types.size(); i++)
unifyType(getNode("type", templateParameters[i]), types[i], &templateTypeMap, typeMap);
for (auto instantiationParam : getNodes("template_param", getNode("template_dec", templateSyntaxTree)))
templateInstantiationTypes->push_back(templateTypeMap[concatSymbolTree(instantiationParam)].clone());
for (auto instantiationParam : getNodes("template_param", getNode("template_dec", templateSyntaxTree))) {
templateInstantiationTypes->push_back(templateTypeMap[concatSymbolTree(getNode("identifier", instantiationParam))].clone()); // gotta be careful of catching the traits in the concat
}
}
//Lookup function for template functions. It has some extra concerns compared to function lookup, namely traits
@@ -1116,10 +1117,17 @@ NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>*
// the current scope, which would happen if we're trying to instantiate a template member function
std::map<std::string, Type*> typeMap = scopeTypeMap;
// If template instantiation was explicit, use those types. Otherwise, unify to find them
if (templateInstantiationTypes->size())
if (templateInstantiationTypes->size()) {
templateInstantiationTypesPerFunction[i] = *templateInstantiationTypes;
else
std::cout << "passed in types" << std::endl;
}else{
unifyTemplateFunction(i, types, &templateInstantiationTypesPerFunction[i], typeMap);
std::cout << "unified types" << std::endl;
}
std::cout << "TYPES ARE: ";
for (Type *a : templateInstantiationTypesPerFunction[i])
std::cout << a->toString() << " : ";
std::cout << std::endl;
auto nameTraitsPairs = makeTemplateNameTraitPairs(templateSyntaxTree->getChildren()[1]);
//Check if sizes match between the placeholder and actual template types
if (nameTraitsPairs.size() != templateInstantiationTypesPerFunction[i].size())
@@ -1131,11 +1139,11 @@ NodeTree<ASTData>* ASTTransformation::templateFunctionLookup(NodeTree<ASTData>*
for (auto j : nameTraitsPairs) {
if (!subset(j.second, templateInstantiationTypesPerFunction[i][typeIndex]->traits)) {
traitsEqual = false;
std::cout << "Traits not a subset for " << j.first << " and " << templateInstantiationTypesPerFunction[i][typeIndex]->toString() << ": ";
std::cout << "Traits not a subset for " << j.first << " and " << templateInstantiationTypesPerFunction[i][typeIndex]->toString() << ": |";
std::copy(j.second.begin(), j.second.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << " vs ";
std::cout << "| vs |";
std::copy(templateInstantiationTypesPerFunction[i][typeIndex]->traits.begin(), templateInstantiationTypesPerFunction[i][typeIndex]->traits.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
std::cout << "|" << std::endl;
break;
} else {
std::cout << "Traits ARE a subset for " << j.first << " and " << templateInstantiationTypesPerFunction[i][typeIndex]->toString() << ": ";

View File

@@ -426,8 +426,8 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
output.preValue += expr.preValue;
std::string retTemp = "ret_temp" + getID();
output.preValue += ValueTypeToCType(children[0]->getDataRef()->valueType, retTemp) + ";\n";
if (methodExists(children[0]->getDataRef()->valueType, "copy_construct"))
output.preValue += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&"+retTemp + ", &" + expr.value);
if (methodExists(children[0]->getDataRef()->valueType, "copy_construct", std::vector<Type>{children[0]->getDataRef()->valueType->withIncreasedIndirection()}))
output.preValue += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&"+retTemp + ", &" + expr.value, std::vector<Type>{children[0]->getDataRef()->valueType->withIncreasedIndirection()});
else
output.preValue += retTemp + " = " + expr.value + ";\n";
// move expr post to before return
@@ -477,12 +477,12 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
return ValueTypeToCType(children[0]->getData().valueType, generate(children[0], enclosingObject, justFuncName).oneString()) + "; " + generate(children[1], enclosingObject, true).oneString(true) + "/*Init Position Call*/";
} else {
// copy constructor if of the same type
if (*children[0]->getDataRef()->valueType == *children[1]->getDataRef()->valueType && methodExists(children[1]->getDataRef()->valueType, "copy_construct")) {
if (*children[0]->getDataRef()->valueType == *children[1]->getDataRef()->valueType && methodExists(children[1]->getDataRef()->valueType, "copy_construct", std::vector<Type>{children[1]->getDataRef()->valueType->withIncreasedIndirection()})) {
CCodeTriple toAssign = generate(children[1], enclosingObject, true);
std::string assignedTo = generate(children[0], enclosingObject, justFuncName).oneString();
output.value = toAssign.preValue;
output.value += ValueTypeToCType(children[0]->getData().valueType, assignedTo) + ";\n";
output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssign.value) + ";\n" + output.postValue;
output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssign.value, std::vector<Type>{children[0]->getDataRef()->valueType->withIncreasedIndirection()}) + ";\n" + output.postValue;
output.value += toAssign.postValue;
return output;
} else {
@@ -540,8 +540,7 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!="
|| name == "<" || name == ">" || name == "%" || name == "=" || name == "+=" || name == "-=" || name == "*=" || name == "/=" || name == "||"
|| name == "&&") {
std::cout << "THIS IS IT NAME: " << name << std::endl;
return "((" + generate(children[1], enclosingObject, true).oneString() + ")" + name + "(" + generate(children[2], enclosingObject, true).oneString() + "))";
return "((" + generate(children[1], enclosingObject, true) + ")" + name + "(" + generate(children[2], enclosingObject, true) + "))";
} else if (name == "." || name == "->") {
if (children.size() == 1)
return "/*dot operation with one child*/" + generate(children[0], enclosingObject, true).oneString() + "/*end one child*/";
@@ -603,12 +602,12 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
}
// see if we should copy_construct all the parameters
for (int i = 1; i < children.size(); i++) { //children[0] is the declaration
if (methodExists(children[i]->getDataRef()->valueType, "copy_construct")) {
if (methodExists(children[i]->getDataRef()->valueType, "copy_construct", std::vector<Type>{children[i]->getDataRef()->valueType->withIncreasedIndirection()})) {
std::string tmpParamName = "param" + getID();
CCodeTriple paramValue = generate(children[i], enclosingObject, true);
output.preValue += paramValue.preValue;
output.preValue += ValueTypeToCType(children[i]->getDataRef()->valueType, tmpParamName) + ";\n";
output.preValue += generateMethodIfExists(children[i]->getDataRef()->valueType, "copy_construct", "&"+tmpParamName + ", &" + paramValue.value);
output.preValue += generateMethodIfExists(children[i]->getDataRef()->valueType, "copy_construct", "&"+tmpParamName + ", &" + paramValue.value, std::vector<Type>{children[i]->getDataRef()->valueType->withIncreasedIndirection()});
output.value += tmpParamName;
output.postValue += paramValue.postValue;
} else {
@@ -620,11 +619,11 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
output += ") ";
// see if we should add a destructer call to this postValue
Type* retType = children[0]->getDataRef()->valueType->returnType;
if (methodExists(retType, "destruct")) {
if (methodExists(retType, "destruct", std::vector<Type>())) {
std::string retTempName = "return_temp" + getID();
output.preValue += ValueTypeToCType(retType, retTempName) + " = " + output.value + ";\n";
output.value = retTempName;
output.postValue = generateMethodIfExists(retType, "destruct", "&"+retTempName) + ";\n" + output.postValue;
output.postValue = generateMethodIfExists(retType, "destruct", "&"+retTempName, std::vector<Type>()) + ";\n" + output.postValue;
}
return output;
}
@@ -674,32 +673,44 @@ std::string CGenerator::generateObjectMethod(NodeTree<ASTData>* enclosingObject,
return output;
}
NodeTree<ASTData>* CGenerator::getMethod(Type* type, std::string method) {
NodeTree<ASTData>* CGenerator::getMethod(Type* type, std::string method, std::vector<Type> types) {
if (type->getIndirection())
return nullptr;
NodeTree<ASTData> *typeDefinition = type->typeDefinition;
if (typeDefinition) {
auto definitionItr = typeDefinition->getDataRef()->scope.find(method);
if (definitionItr != typeDefinition->getDataRef()->scope.end())
return definitionItr->second[0];
if (definitionItr != typeDefinition->getDataRef()->scope.end()) {
for (auto method : definitionItr->second) {
bool methodFits = true;
std::vector<Type> methodTypes = dereferenced(method->getDataRef()->valueType->parameterTypes);
if (types.size() != methodTypes.size())
continue;
for (int i = 0; i < types.size(); i++) {
if (types[i] != methodTypes[i]) {
methodFits = false;
break;
}
}
if (methodFits)
return method;
}
}
}
return nullptr;
}
bool CGenerator::methodExists(Type* type, std::string method) {
return getMethod(type, method) != nullptr;
bool CGenerator::methodExists(Type* type, std::string method, std::vector<Type> types) {
return getMethod(type, method, types) != nullptr;
}
std::string CGenerator::generateMethodIfExists(Type* type, std::string method, std::string parameter) {
NodeTree<ASTData> *typeDefinition = type->typeDefinition;
NodeTree<ASTData> *methodDef = getMethod(type, method);
std::string CGenerator::generateMethodIfExists(Type* type, std::string method, std::string parameter, std::vector<Type> methodTypes) {
NodeTree<ASTData> *methodDef = getMethod(type, method, methodTypes);
if (methodDef) {
NodeTree<ASTData> *typeDefinition = type->typeDefinition;
std::string nameDecoration;
for (Type *paramType : methodDef->getDataRef()->valueType->parameterTypes)
nameDecoration += "_" + ValueTypeToCTypeDecoration(paramType);
return scopePrefix(typeDefinition) + CifyName(typeDefinition->getDataRef()->symbol.getName()) + "__" + method + nameDecoration + "(" + parameter + ");\n";
} else {
std::cout << method << " DOESN'T EXIST FOR TYPE " << type->toString() << std::endl;
}
return "";
}
@@ -707,7 +718,7 @@ std::string CGenerator::generateMethodIfExists(Type* type, std::string method, s
std::string CGenerator::emitDestructors(std::vector<NodeTree<ASTData>*> identifiers, NodeTree<ASTData>* enclosingObject) {
std::string destructorString = "";
for (auto identifier : identifiers)
destructorString += tabs() + generateMethodIfExists(identifier->getDataRef()->valueType, "destruct", "&" + generate(identifier, enclosingObject).oneString());
destructorString += tabs() + generateMethodIfExists(identifier->getDataRef()->valueType, "destruct", "&" + generate(identifier, enclosingObject).oneString(), std::vector<Type>());
return destructorString;
}

View File

@@ -169,3 +169,15 @@ void Type::decreaseIndirection() {
void Type::modifyIndirection(int mod) {
setIndirection(indirection + mod);
}
Type Type::withIncreasedIndirection() {
Type *newOne = clone();
newOne->increaseIndirection();
return *newOne;
}
Type Type::withDecreasedIndirection() {
Type *newOne = clone();
newOne->decreaseIndirection();
return *newOne;
}