diff --git a/ctx.cpp b/ctx.cpp index 98c8ec5c..b041c03d 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -1927,6 +1927,11 @@ FunctionEmitContext::BinaryOperator(llvm::Instruction::BinaryOps inst, return NULL; } + if (v0->getType() != v1->getType()) { + v0->dump(); + printf("\n\n"); + v1->dump(); + } AssertPos(currentPos, v0->getType() == v1->getType()); llvm::Type *type = v0->getType(); int arraySize = lArrayVectorWidth(type); diff --git a/expr.cpp b/expr.cpp index 04a28d40..1e8748e2 100644 --- a/expr.cpp +++ b/expr.cpp @@ -561,6 +561,7 @@ lDoTypeConv(const Type *fromType, const Type *toType, Expr **expr, "\"%s\" for %s", fromType->GetString().c_str(), toPolyType->GetString().c_str(), errorMsgBase); } + return false; } } @@ -7148,8 +7149,11 @@ TypeCastExpr::GetValue(FunctionEmitContext *ctx) const { return NULL; return ctx->IntToPtrInst(exprVal, llvmToType, "int_to_ptr"); - } - else { + } else if (CastType(toType)) { + Error(pos, "Unexpected polymorphic type cast to \"%s\"", + toType->GetString().c_str()); + return NULL; + } else { const AtomicType *toAtomic = CastType(toType); // typechecking should ensure this is the case if (!toAtomic) { diff --git a/func.cpp b/func.cpp index fa1e030d..8776a9fc 100644 --- a/func.cpp +++ b/func.cpp @@ -651,8 +651,10 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const { const FunctionType *func = CastType(sym->type); - printf("%s before replacing anything:\n", sym->name.c_str()); - code->Print(0); + if (g->debugPrint) { + printf("%s before replacing anything:\n", sym->name.c_str()); + code->Print(0); + } for (size_t i=0; i(versions[i]->type); @@ -665,13 +667,15 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const { ncode = (Stmt*)TranslatePoly(ncode, from, ft->GetParameterType(j)->GetBaseType()); - printf("%s after replacing %s with %s:\n\n", - sym->name.c_str(), from->GetString().c_str(), - ft->GetParameterType(j)->GetBaseType()->GetString().c_str()); + if (g->debugPrint) { + printf("%s after replacing %s with %s:\n\n", + sym->name.c_str(), from->GetString().c_str(), + ft->GetParameterType(j)->GetBaseType()->GetString().c_str()); - ncode->Print(0); + ncode->Print(0); - printf("------------------------------------------\n\n"); + printf("------------------------------------------\n\n"); + } } } diff --git a/module.cpp b/module.cpp index 4a5dcbe5..4d885200 100644 --- a/module.cpp +++ b/module.cpp @@ -1032,8 +1032,7 @@ Module::AddFunctionDeclaration(const std::string &name, } std::vector nextExpanded; - std::set::iterator iter; - for (iter = toExpand.begin(); iter != toExpand.end(); iter++) { + for (auto iter = toExpand.begin(); iter != toExpand.end(); iter++) { for (size_t j=0; jGetParameterSourcePos(k)); } - nextExpanded.push_back(new FunctionType(eft->GetReturnType(), + const Type *ret = eft->GetReturnType(); + if (Type::EqualForReplacement(ret, pt)) { + printf("Replaced return type %s\n", + ret->GetString().c_str()); + ret = PolyType::ReplaceType(ret, *te); + } + + nextExpanded.push_back(new FunctionType(ret, nargs, nargsn, nargsd, @@ -1078,6 +1084,11 @@ Module::AddFunctionDeclaration(const std::string &name, if (expanded.size() > 1) { for (size_t i=0; iGetReturnType()->IsPolymorphicType()) { + Error(pos, "Unexpected polymorphic return type \"%s\"", + expanded[i]->GetReturnType()->GetString().c_str()); + return; + } std::string nname = name; if (functionType->isExported || functionType->isExternC) { for (int j=0; jGetNumParameters(); j++) { diff --git a/sym.cpp b/sym.cpp index e22105eb..48ee06f7 100644 --- a/sym.cpp +++ b/sym.cpp @@ -147,7 +147,7 @@ bool SymbolTable::AddFunction(Symbol *symbol) { const FunctionType *ft = CastType(symbol->type); Assert(ft != NULL); - if (LookupFunction(symbol->name.c_str(), ft) != NULL) + if (LookupFunction(symbol->name.c_str(), ft, true) != NULL) // A function of the same name and type has already been added to // the symbol table return false; @@ -183,7 +183,8 @@ SymbolTable::LookupFunction(const char *name, std::vector *matches) { Symbol * -SymbolTable::LookupFunction(const char *name, const FunctionType *type) { +SymbolTable::LookupFunction(const char *name, const FunctionType *type, + bool ignorePoly) { FunctionMapType::iterator iter = functions.find(name); if (iter != functions.end()) { std::vector funcs = iter->second; @@ -193,7 +194,7 @@ SymbolTable::LookupFunction(const char *name, const FunctionType *type) { } } // Try looking for a polymorphic function - if (polyFunctions[name].size() > 0) { + if (!ignorePoly && polyFunctions[name].size() > 0) { std::string n = name; return new Symbol(name, polyFunctions[name][0]->pos, type); } diff --git a/sym.h b/sym.h index 46c6fe9e..41973c72 100644 --- a/sym.h +++ b/sym.h @@ -181,7 +181,8 @@ public: in the symbol table. @return pointer to matching Symbol; NULL if none is found. */ - Symbol *LookupFunction(const char *name, const FunctionType *type); + Symbol *LookupFunction(const char *name, const FunctionType *type, + bool ignorePoly = false); std::vector& LookupPolyFunction(const char *name); diff --git a/type.cpp b/type.cpp index 2f102dfc..3c421bf4 100644 --- a/type.cpp +++ b/type.cpp @@ -4078,7 +4078,8 @@ bool Type::IsBasicType(const Type *type) { return (CastType(type) != NULL || CastType(type) != NULL || - CastType(type) != NULL); + CastType(type) != NULL || + CastType(type) != NULL); }