pass by reference should work for templates and objects now
This commit is contained in:
@@ -1521,6 +1521,7 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree<Symbol>* typeNode, NodeTree<A
|
|||||||
std::cout << "Template type! (" << edited << ")" << std::endl;
|
std::cout << "Template type! (" << edited << ")" << std::endl;
|
||||||
Type* templateTypeReplacement = templateTypeReplacements[edited]->clone();
|
Type* templateTypeReplacement = templateTypeReplacements[edited]->clone();
|
||||||
templateTypeReplacement->modifyIndirection(indirection);
|
templateTypeReplacement->modifyIndirection(indirection);
|
||||||
|
templateTypeReplacement->is_reference = is_reference;
|
||||||
return templateTypeReplacement;
|
return templateTypeReplacement;
|
||||||
}
|
}
|
||||||
std::cout << edited << " was not found in templateTypeReplacements" << std::endl;
|
std::cout << edited << " was not found in templateTypeReplacements" << std::endl;
|
||||||
|
|||||||
@@ -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
|
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* func_param_type = children[0]->getDataRef()->valueType->parameterTypes[i-1];
|
||||||
Type *param_type = children[i]->getDataRef()->valueType;
|
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();
|
std::string tmpParamName = "param" + getID();
|
||||||
CCodeTriple paramValue = generate(children[i], enclosingObject, true, enclosingFunction);
|
CCodeTriple paramValue = generate(children[i], enclosingObject, true, enclosingFunction);
|
||||||
parameters.preValue += paramValue.preValue;
|
parameters.preValue += paramValue.preValue;
|
||||||
parameters.preValue += ValueTypeToCType(param_type, tmpParamName) + ";\n";
|
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()});
|
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;
|
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 {
|
} else {
|
||||||
parameters += generate(children[i], enclosingObject, true, enclosingFunction);
|
parameters += generate(children[i], enclosingObject, true, enclosingFunction);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (i < children.size()-1)
|
if (i < children.size()-1)
|
||||||
parameters += ", ";
|
parameters += ", ";
|
||||||
@@ -909,6 +900,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 CGenerator::emitDestructors(std::vector<NodeTree<ASTData>*> identifiers, NodeTree<ASTData>* enclosingObject) {
|
||||||
std::string destructorString = "";
|
std::string destructorString = "";
|
||||||
for (auto identifier : identifiers)
|
for (auto identifier : identifiers)
|
||||||
|
if (!identifier->getDataRef()->valueType->is_reference)
|
||||||
destructorString += tabs() + generateMethodIfExists(identifier->getDataRef()->valueType, "destruct", "&" + generate(identifier, enclosingObject).oneString(), std::vector<Type>());
|
destructorString += tabs() + generateMethodIfExists(identifier->getDataRef()->valueType, "destruct", "&" + generate(identifier, enclosingObject).oneString(), std::vector<Type>());
|
||||||
return destructorString;
|
return destructorString;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
7
|
7
|
||||||
|
8
|
||||||
|
7.700000
|
||||||
|
construct
|
||||||
|
do
|
||||||
|
destruct
|
||||||
|
|||||||
@@ -1,12 +1,34 @@
|
|||||||
import io:*
|
import io:*
|
||||||
|
|
||||||
/*fun byRef<T>(it: ref T) {*/
|
obj test_cons(Object) {
|
||||||
/*it = it + 1*/
|
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) {*/
|
fun call<T>(it: ref T) {
|
||||||
/**it = *it + 1*/
|
it.do()
|
||||||
/*}*/
|
}
|
||||||
|
|
||||||
|
fun byRef<T>(it: ref T) {
|
||||||
|
it = it + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun byRef(it: * int) {
|
||||||
|
*it = *it + 1
|
||||||
|
}
|
||||||
|
|
||||||
fun byRef(it: ref int) {
|
fun byRef(it: ref int) {
|
||||||
it = it + 1
|
it = it + 1
|
||||||
@@ -18,6 +40,14 @@ fun main():int {
|
|||||||
var a = 6
|
var a = 6
|
||||||
byRef(a)
|
byRef(a)
|
||||||
println(a)
|
println(a)
|
||||||
|
byRef(&a)
|
||||||
|
println(a)
|
||||||
|
var b = 6.7
|
||||||
|
byRef(b)
|
||||||
|
println(b)
|
||||||
|
|
||||||
|
var t.construct() : test_cons
|
||||||
|
call(t)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user