[WIP] implement ReplacePolyType for stmts

This commit is contained in:
2017-05-09 15:30:39 -04:00
parent aeb4c0b6f9
commit 7bb1741b9a
3 changed files with 158 additions and 43 deletions

View File

@@ -915,6 +915,8 @@ Module::AddFunctionDeclaration(const std::string &name,
SourcePos pos) {
Assert(functionType != NULL);
fprintf(stderr, "Adding %s\n", name.c_str());
// If a global variable with the same name has already been declared
// issue an error.
if (symbolTable->LookupVariable(name.c_str()) != NULL) {
@@ -1009,6 +1011,90 @@ Module::AddFunctionDeclaration(const std::string &name,
}
}
/* Handle Polymorphic functions
* a function
* int foo(number n, floating, f)
* will produce versions such as
* int foo(int n, float f)
*
* these functions will be overloaded if they are not exported, or mangled
* if exported */
std::vector<int> toExpand;
std::vector<const FunctionType *> expanded;
expanded.push_back(functionType);
for (int i=0; i<functionType->GetNumParameters(); i++) {
if (functionType->GetParameterType(i)->IsPolymorphicType()) {
fprintf(stderr, "Expanding polymorphic function \"%s\"\n",
name.c_str());
toExpand.push_back(i);
}
}
std::vector<const FunctionType *> nextExpanded;
for (size_t i=0; i<toExpand.size(); i++) {
for (size_t j=0; j<expanded.size(); j++) {
const FunctionType *eft = expanded[j];
const PolyType *pt=CastType<PolyType>(
eft->GetParameterType(toExpand[i])->GetBaseType());
std::vector<AtomicType *>::iterator te;
for (te = pt->ExpandBegin(); te != pt->ExpandEnd(); te++) {
llvm::SmallVector<const Type *, 8> nargs;
llvm::SmallVector<std::string, 8> nargsn;
llvm::SmallVector<Expr *, 8> nargsd;
llvm::SmallVector<SourcePos, 8> nargsp;
for (size_t k=0; k<eft->GetNumParameters(); k++) {
if (k == toExpand[i]) {
const Type *r;
r = PolyType::ReplaceType(eft->GetParameterType(j),*te);
nargs.push_back(r);
} else {
nargs.push_back(eft->GetParameterType(k));
}
nargsn.push_back(eft->GetParameterName(k));
nargsd.push_back(eft->GetParameterDefault(k));
nargsp.push_back(eft->GetParameterSourcePos(k));
}
nextExpanded.push_back(new FunctionType(eft->GetReturnType(),
nargs,
nargsn,
nargsd,
nargsp,
eft->isTask,
eft->isExported,
eft->isExternC,
eft->isUnmasked));
}
}
expanded.swap(nextExpanded);
nextExpanded.clear();
}
if (expanded.size() > 1) {
for (size_t i=0; i<expanded.size(); i++) {
std::string nname = name;
if (functionType->isExported || functionType->isExternC) {
for (int j=0; j<expanded[i]->GetNumParameters(); j++) {
nname += "_";
nname += expanded[i]->GetParameterType(j)->Mangle();
}
}
fprintf(stderr, "Adding expanded function %s\n", nname.c_str());
AddFunctionDeclaration(nname, expanded[i], storageClass,
isInline, pos);
}
return;
}
// Get the LLVM FunctionType
bool disableMask = (storageClass == SC_EXTERN_C);
llvm::FunctionType *llvmFunctionType =

View File

@@ -499,6 +499,18 @@ DeclStmt::TypeCheck() {
return encounteredError ? NULL : this;
}
Stmt *
DeclStmt::ReplacePolyType(const PolyType *from, const Type *to) {
for (size_t i = 0; i < vars.size(); i++) {
Symbol *s = vars[i].sym;
if (Type::EqualIgnoringConst(s->type->GetBaseType(), from)) {
s->type = PolyType::ReplaceType(s->type, to);
}
}
return this;
}
void
DeclStmt::Print(int indent) const {
@@ -2179,6 +2191,21 @@ ForeachStmt::TypeCheck() {
return anyErrors ? NULL : this;
}
Stmt *
ForeachStmt::ReplacePolyType(const PolyType *from, const Type *to) {
if (!stmts)
return NULL;
for (size_t i=0; i<dimVariables.size(); i++) {
const Type *t = dimVariables[i]->type;
if (Type::EqualIgnoringConst(t->GetBaseType(), from)) {
t = PolyType::ReplaceType(t, to);
}
}
return this;
}
int
ForeachStmt::EstimateCost() const {

2
stmt.h
View File

@@ -118,6 +118,7 @@ public:
Stmt *Optimize();
Stmt *TypeCheck();
Stmt *ReplacePolyType(const PolyType *from, const Type *to);
int EstimateCost() const;
std::vector<VariableDeclaration> vars;
@@ -282,6 +283,7 @@ public:
void Print(int indent) const;
Stmt *TypeCheck();
Stmt *ReplacePolyType(const PolyType *from, const Type *to);
int EstimateCost() const;
std::vector<Symbol *> dimVariables;