pass by reference should work for templates and objects now

This commit is contained in:
Nathan Braswell
2015-07-14 22:42:25 -04:00
parent 602810002b
commit 0ee44e829f
4 changed files with 50 additions and 22 deletions

View File

@@ -1521,6 +1521,7 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree<Symbol>* typeNode, NodeTree<A
std::cout << "Template type! (" << edited << ")" << std::endl;
Type* templateTypeReplacement = templateTypeReplacements[edited]->clone();
templateTypeReplacement->modifyIndirection(indirection);
templateTypeReplacement->is_reference = is_reference;
return templateTypeReplacement;
}
std::cout << edited << " was not found in templateTypeReplacements" << std::endl;

View File

@@ -743,28 +743,19 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
for (int i = 1; i < children.size(); i++) { //children[0] is the declaration
Type* func_param_type = children[0]->getDataRef()->valueType->parameterTypes[i-1];
Type *param_type = children[i]->getDataRef()->valueType;
if (methodExists(children[i]->getDataRef()->valueType, "copy_construct", std::vector<Type>{children[i]->getDataRef()->valueType->withIncreasedIndirection()})) {
// don't copy_construct references
if (func_param_type->is_reference) {
parameters += "&" + generate(children[i], enclosingObject, true, enclosingFunction);
} else 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, enclosingFunction);
parameters.preValue += paramValue.preValue;
parameters.preValue += ValueTypeToCType(param_type, tmpParamName) + ";\n";
parameters.preValue += generateMethodIfExists(param_type, "copy_construct", "&"+tmpParamName + ", &" + paramValue.value, std::vector<Type>{children[i]->getDataRef()->valueType->withIncreasedIndirection()});
if (func_param_type->is_reference)
parameters.value += "&" + tmpParamName;
else
parameters.value += tmpParamName;
parameters.value += tmpParamName;
parameters.postValue += paramValue.postValue;
} else {
if (func_param_type->is_reference) {
//std::string tmpParamName = "param" + getID();
//CCodeTriple paramValue = generate(children[i], enclosingObject, true, enclosingFunction);
//parameters.preValue += paramValue.preValue;
//parameters.preValue += ValueTypeToCType(param_type, tmpParamName) + " = " + paramValue.value + ";\n";
//parameters.value += "&" + tmpParamName;
parameters += "&" + generate(children[i], enclosingObject, true, enclosingFunction);
} else {
parameters += generate(children[i], enclosingObject, true, enclosingFunction);
}
parameters += generate(children[i], enclosingObject, true, enclosingFunction);
}
if (i < children.size()-1)
parameters += ", ";
@@ -909,7 +900,8 @@ 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(), std::vector<Type>());
if (!identifier->getDataRef()->valueType->is_reference)
destructorString += tabs() + generateMethodIfExists(identifier->getDataRef()->valueType, "destruct", "&" + generate(identifier, enclosingObject).oneString(), std::vector<Type>());
return destructorString;
}

View File

@@ -1 +1,6 @@
7
8
7.700000
construct
do
destruct

View File

@@ -1,12 +1,34 @@
import io:*
/*fun byRef<T>(it: ref T) {*/
/*it = it + 1*/
/*}*/
obj test_cons(Object) {
fun construct(): *test_cons {
println("construct")
}
fun copy_construct(other: *test_cons) {
println("copy_construct")
}
fun destruct() {
println("destruct")
}
fun operator=(other: test_cons) {
println("=")
}
fun do() {
println("do")
}
}
/*fun byRef(it: * int) {*/
/**it = *it + 1*/
/*}*/
fun call<T>(it: ref T) {
it.do()
}
fun byRef<T>(it: ref T) {
it = it + 1
}
fun byRef(it: * int) {
*it = *it + 1
}
fun byRef(it: ref int) {
it = it + 1
@@ -18,6 +40,14 @@ fun main():int {
var a = 6
byRef(a)
println(a)
byRef(&a)
println(a)
var b = 6.7
byRef(b)
println(b)
var t.construct() : test_cons
call(t)
return 0
}