2021-05-25 02:20:23 +00:00
|
|
|
#include "typeutils.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void ConvertIdCustomsToGenerics(IdNode *node) {
|
|
|
|
uint32_t i;
|
|
|
|
switch(node->type)
|
|
|
|
{
|
|
|
|
case UnorderedScope:
|
|
|
|
case OrderedScope:
|
|
|
|
case Struct:
|
|
|
|
/* FIXME: This case will need to be modified to handle type parameters over structs. */
|
|
|
|
for (i = 0; i < node->childCount; i += 1) {
|
|
|
|
ConvertIdCustomsToGenerics(node->children[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Variable: {
|
|
|
|
TypeTag *varType = node->typeTag;
|
|
|
|
if (varType->type == Custom) {
|
|
|
|
IdNode *x = LookupId(node->parent, node, varType->value.customType);
|
|
|
|
if (x != NULL && x->type == GenericType) {
|
|
|
|
varType->type = Generic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Function: {
|
|
|
|
TypeTag *funcType = node->typeTag;
|
|
|
|
if (funcType->type == Custom) {
|
|
|
|
/* For functions we have to handle the type lookup manually since the generic type
|
|
|
|
* identifiers are declared as children of the function's IdNode. */
|
|
|
|
for (i = 0; i < node->childCount; i += 1) {
|
|
|
|
IdNode *child = node->children[i];
|
|
|
|
if (child->type == GenericType && strcmp(child->name, funcType->value.customType) == 0) {
|
|
|
|
funcType->type = Generic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < node->childCount; i += 1) {
|
|
|
|
ConvertIdCustomsToGenerics(node->children[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertASTCustomsToGenerics(Node *node) {
|
|
|
|
uint32_t i;
|
2021-05-26 21:43:51 +00:00
|
|
|
switch (node->syntaxKind)
|
|
|
|
{
|
|
|
|
case Declaration:
|
|
|
|
{
|
|
|
|
Node *type = node->declaration.type->type.typeNode;
|
|
|
|
Node *id = node->declaration.identifier;
|
|
|
|
if (id->typeTag->type == Generic && type->syntaxKind == CustomTypeNode) {
|
|
|
|
free(node->declaration.type);
|
|
|
|
node->declaration.type = MakeGenericTypeNode(id->typeTag->value.genericType);
|
2021-05-25 02:20:23 +00:00
|
|
|
}
|
2021-05-26 21:43:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-25 02:20:23 +00:00
|
|
|
|
2021-05-26 21:43:51 +00:00
|
|
|
case FunctionSignature:
|
|
|
|
{
|
|
|
|
Node *id = node->functionSignature.identifier;
|
|
|
|
Node *type = node->functionSignature.type;
|
|
|
|
if (id->typeTag->type == Generic && type->syntaxKind == CustomTypeNode) {
|
|
|
|
free(node->functionSignature.type);
|
|
|
|
node->functionSignature.type = MakeGenericTypeNode(id->typeTag->value.genericType);
|
2021-05-25 02:20:23 +00:00
|
|
|
}
|
2021-05-26 21:43:51 +00:00
|
|
|
ConvertASTCustomsToGenerics(node->functionSignature.arguments);
|
|
|
|
return;
|
|
|
|
}
|
2021-05-25 02:20:23 +00:00
|
|
|
|
2021-05-26 21:43:51 +00:00
|
|
|
default:
|
|
|
|
recurse(node, *ConvertASTCustomsToGenerics);
|
2021-05-25 02:20:23 +00:00
|
|
|
}
|
|
|
|
}
|