Ok, full functions as values support with types and fixes of earlier bugs. Now typedef the function pointer types when generateing C.
This commit is contained in:
@@ -904,29 +904,23 @@ NodeTree<ASTData>* ASTTransformation::functionLookup(NodeTree<ASTData>* scope, s
|
||||
//We're not looking for types
|
||||
if (i->getDataRef()->type == type_def)
|
||||
continue;
|
||||
Type* functionType = i->getDataRef()->valueType;
|
||||
|
||||
std::vector<NodeTree<ASTData>*> children = i->getChildren();
|
||||
//We subtract one from the children to get the type size only if there is at least one child AND
|
||||
// the last node is actually a body node, as it may not have been generated yet if we're in the body
|
||||
//and this function is recursive or if this is a non-instantiated template function
|
||||
int numTypes = (children.size() > 0 && children[children.size()-1]->getDataRef()->type == code_block) ? children.size()-1 : children.size();
|
||||
int numTypes = functionType->parameterTypes.size();
|
||||
if (types.size() != numTypes) {
|
||||
std::cout << "Type sizes do not match between two " << lookup << "(" << types.size() << "," << numTypes << "), types are: ";
|
||||
for (auto j : types)
|
||||
std::cout << j.toString() << " ";
|
||||
std::cout << std::endl;
|
||||
std::cout << "Versus" << std::endl;
|
||||
for (int j = 0; j < numTypes; j++) {
|
||||
std::cout << " vs " << children[j]->getDataRef()->valueType->toString() << std::endl;
|
||||
}
|
||||
for (auto child: children)
|
||||
std::cout << "\t" << child->getDataRef()->toString() << std::endl;
|
||||
for (int j = 0; j < numTypes; j++)
|
||||
std::cout << functionType->parameterTypes[j]->toString() << " ";
|
||||
std::cout << std::endl;
|
||||
continue;
|
||||
}
|
||||
bool typesMatch = true;
|
||||
for (int j = 0; j < types.size(); j++) {
|
||||
Type* tmpType = children[j]->getDataRef()->valueType;
|
||||
Type* tmpType = functionType->parameterTypes[j];
|
||||
//Don't worry if types don't match if it's a template type
|
||||
//if (types[j] != *tmpType && tmpType->baseType != template_type_type) {
|
||||
// WE DO WORRY NOW B/C template type infrence is ugly and we need this to fail
|
||||
@@ -934,7 +928,7 @@ NodeTree<ASTData>* ASTTransformation::functionLookup(NodeTree<ASTData>* scope, s
|
||||
if (types[j] != *tmpType) {
|
||||
typesMatch = false;
|
||||
std::cout << "Types do not match between two " << lookup << " " << types[j].toString();
|
||||
std::cout << " vs " << children[j]->getDataRef()->valueType->toString() << std::endl;
|
||||
std::cout << " vs " << tmpType->toString() << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1277,7 +1271,11 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree<Symbol>* typeNode, NodeTree<A
|
||||
ValueType baseType;
|
||||
NodeTree<ASTData>* typeDefinition = NULL;
|
||||
std::set<std::string> traits;
|
||||
while (typeIn[typeIn.size() - indirection - 1] == '*') indirection++;
|
||||
// To counter this, for every indirection we step down a level
|
||||
while (typeIn[typeIn.size() - indirection - 1] == '*') {
|
||||
indirection++;
|
||||
typeNode = typeNode->getChildren()[0];
|
||||
};
|
||||
std::string edited = strSlice(typeIn, 0, -(indirection + 1));
|
||||
if (edited == "void")
|
||||
baseType = void_type;
|
||||
@@ -1291,7 +1289,13 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree<Symbol>* typeNode, NodeTree<A
|
||||
baseType = double_percision;
|
||||
else if (edited == "char")
|
||||
baseType = character;
|
||||
else {
|
||||
else if (typeNode->getChildren().size() && typeNode->getChildren()[0]->getDataRef()->getName() == "function_type") {
|
||||
baseType = function_type;
|
||||
std::vector<Type*> types;
|
||||
for (auto typeSyntaxNode : getNodes("type", typeNode->getChildren()[0]->getChildren()))
|
||||
types.push_back(typeFromTypeNode(typeSyntaxNode, scope, templateTypeReplacements));
|
||||
return new Type(slice(types, 0, -2),types.back());
|
||||
} else {
|
||||
baseType = none;
|
||||
|
||||
auto possibleMatches = scopeLookup(scope, edited);
|
||||
@@ -1313,11 +1317,6 @@ Type* ASTTransformation::typeFromTypeNode(NodeTree<Symbol>* typeNode, NodeTree<A
|
||||
std::cout << i.first << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
// getChildren()[1] is \* because of pointer instead of template_inst
|
||||
// To counter this, for every indirection we step down a level
|
||||
for (int i = 0; i < indirection; i++)
|
||||
typeNode = typeNode->getChildren()[0];
|
||||
|
||||
std::cout << possibleMatches.size() << " " << typeNode->getChildren().size() << std::endl;
|
||||
if (typeNode->getChildren().size() > 1)
|
||||
std::cout << typeNode->getChildren()[1]->getDataRef()->getName() << std::endl;
|
||||
|
||||
@@ -126,6 +126,9 @@ std::pair<std::string, std::string> CGenerator::generateTranslationUnit(std::str
|
||||
std::string classStructs = "/**\n * Class Structs\n */\n\n";
|
||||
std::string functionPrototypes = "/**\n * Function Prototypes\n */\n\n";
|
||||
std::string functionDefinitions = "/**\n * Function Definitions\n */\n\n";
|
||||
// There also exists functionTypedefString which is a member variable that keeps
|
||||
// track of utility typedefs that allow our C type generation to be more sane
|
||||
// it is emitted in the h file right before functionPrototypes
|
||||
|
||||
|
||||
// And get the correct order for emiting classes, but not if they're not in our file, then they will get included
|
||||
@@ -242,7 +245,7 @@ std::pair<std::string, std::string> CGenerator::generateTranslationUnit(std::str
|
||||
}
|
||||
}
|
||||
}
|
||||
hOutput += plainTypedefs + importIncludes + topLevelCPassthrough + variableExternDeclarations + classStructs + functionPrototypes;
|
||||
hOutput += plainTypedefs + importIncludes + topLevelCPassthrough + variableExternDeclarations + classStructs + functionTypedefString + functionPrototypes;
|
||||
cOutput += variableDeclarations + functionDefinitions;
|
||||
return std::make_pair(hOutput, cOutput);
|
||||
}
|
||||
@@ -568,19 +571,20 @@ std::string CGenerator::ValueTypeToCTypeThingHelper(Type *type, std::string decl
|
||||
case function_type:
|
||||
{
|
||||
std::string indr_str;
|
||||
std::string typedefStr = "typedef ";
|
||||
std::string typedefID = "ID" + CifyName(type->toString(false));
|
||||
for (int i = 0; i < type->getIndirection(); i++)
|
||||
indr_str += "*";
|
||||
return_type = ValueTypeToCTypeThingHelper(type->returnType, "");
|
||||
if (type->getIndirection())
|
||||
return_type += " (" + indr_str + "(*" + declaration + "))(";
|
||||
else
|
||||
return_type += " (*" + declaration + ")(";
|
||||
typedefStr += ValueTypeToCTypeThingHelper(type->returnType, "");
|
||||
typedefStr += " (*" + typedefID + ")(";
|
||||
if (type->parameterTypes.size() == 0)
|
||||
return_type += "void";
|
||||
typedefStr += "void";
|
||||
else
|
||||
for (int i = 0; i < type->parameterTypes.size(); i++)
|
||||
return_type += (i != 0 ? ", " : "") + ValueTypeToCTypeThingHelper(type->parameterTypes[i], "");
|
||||
return_type += ")";
|
||||
typedefStr += (i != 0 ? ", " : "") + ValueTypeToCTypeThingHelper(type->parameterTypes[i], "");
|
||||
typedefStr += ");\n";
|
||||
functionTypedefString += typedefStr;
|
||||
return_type = typedefID + indr_str + " " + declaration;
|
||||
do_ending = false;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -17,8 +17,6 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths, std:
|
||||
removeSymbols.push_back(Symbol("WS", false));
|
||||
removeSymbols.push_back(Symbol("\\(", true));
|
||||
removeSymbols.push_back(Symbol("\\)", true));
|
||||
//removeSymbols.push_back(Symbol("::", true));
|
||||
//removeSymbols.push_back(Symbol(":", true));
|
||||
removeSymbols.push_back(Symbol("var", true));
|
||||
removeSymbols.push_back(Symbol("fun", true));
|
||||
|
||||
@@ -53,6 +51,7 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths, std:
|
||||
collapseSymbols.push_back(Symbol("if_comp_pred", false));
|
||||
collapseSymbols.push_back(Symbol("declaration_block", false));
|
||||
collapseSymbols.push_back(Symbol("type_list", false));
|
||||
collapseSymbols.push_back(Symbol("opt_type_list", false));
|
||||
collapseSymbols.push_back(Symbol("template_param_list", false));
|
||||
collapseSymbols.push_back(Symbol("trait_list", false));
|
||||
collapseSymbols.push_back(Symbol("dec_type", false));
|
||||
|
||||
Reference in New Issue
Block a user