Typechecking fixes, moved some printing behind debug flag

This commit is contained in:
2017-05-10 23:12:48 -04:00
parent ab29965d75
commit d020107d91
7 changed files with 44 additions and 17 deletions

View File

@@ -1927,6 +1927,11 @@ FunctionEmitContext::BinaryOperator(llvm::Instruction::BinaryOps inst,
return NULL; return NULL;
} }
if (v0->getType() != v1->getType()) {
v0->dump();
printf("\n\n");
v1->dump();
}
AssertPos(currentPos, v0->getType() == v1->getType()); AssertPos(currentPos, v0->getType() == v1->getType());
llvm::Type *type = v0->getType(); llvm::Type *type = v0->getType();
int arraySize = lArrayVectorWidth(type); int arraySize = lArrayVectorWidth(type);

View File

@@ -561,6 +561,7 @@ lDoTypeConv(const Type *fromType, const Type *toType, Expr **expr,
"\"%s\" for %s", fromType->GetString().c_str(), "\"%s\" for %s", fromType->GetString().c_str(),
toPolyType->GetString().c_str(), errorMsgBase); toPolyType->GetString().c_str(), errorMsgBase);
} }
return false;
} }
} }
@@ -7148,8 +7149,11 @@ TypeCastExpr::GetValue(FunctionEmitContext *ctx) const {
return NULL; return NULL;
return ctx->IntToPtrInst(exprVal, llvmToType, "int_to_ptr"); return ctx->IntToPtrInst(exprVal, llvmToType, "int_to_ptr");
} } else if (CastType<PolyType>(toType)) {
else { Error(pos, "Unexpected polymorphic type cast to \"%s\"",
toType->GetString().c_str());
return NULL;
} else {
const AtomicType *toAtomic = CastType<AtomicType>(toType); const AtomicType *toAtomic = CastType<AtomicType>(toType);
// typechecking should ensure this is the case // typechecking should ensure this is the case
if (!toAtomic) { if (!toAtomic) {

View File

@@ -651,8 +651,10 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const {
const FunctionType *func = CastType<FunctionType>(sym->type); const FunctionType *func = CastType<FunctionType>(sym->type);
if (g->debugPrint) {
printf("%s before replacing anything:\n", sym->name.c_str()); printf("%s before replacing anything:\n", sym->name.c_str());
code->Print(0); code->Print(0);
}
for (size_t i=0; i<versions.size(); i++) { for (size_t i=0; i<versions.size(); i++) {
const FunctionType *ft = CastType<FunctionType>(versions[i]->type); const FunctionType *ft = CastType<FunctionType>(versions[i]->type);
@@ -665,6 +667,7 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const {
ncode = (Stmt*)TranslatePoly(ncode, from, ncode = (Stmt*)TranslatePoly(ncode, from,
ft->GetParameterType(j)->GetBaseType()); ft->GetParameterType(j)->GetBaseType());
if (g->debugPrint) {
printf("%s after replacing %s with %s:\n\n", printf("%s after replacing %s with %s:\n\n",
sym->name.c_str(), from->GetString().c_str(), sym->name.c_str(), from->GetString().c_str(),
ft->GetParameterType(j)->GetBaseType()->GetString().c_str()); ft->GetParameterType(j)->GetBaseType()->GetString().c_str());
@@ -674,6 +677,7 @@ Function::ExpandPolyArguments(SymbolTable *symbolTable) const {
printf("------------------------------------------\n\n"); printf("------------------------------------------\n\n");
} }
} }
}
Symbol *s = symbolTable->LookupFunction(versions[i]->name.c_str(), ft); Symbol *s = symbolTable->LookupFunction(versions[i]->name.c_str(), ft);

View File

@@ -1032,8 +1032,7 @@ Module::AddFunctionDeclaration(const std::string &name,
} }
std::vector<const FunctionType *> nextExpanded; std::vector<const FunctionType *> nextExpanded;
std::set<const Type*>::iterator iter; for (auto iter = toExpand.begin(); iter != toExpand.end(); iter++) {
for (iter = toExpand.begin(); iter != toExpand.end(); iter++) {
for (size_t j=0; j<expanded.size(); j++) { for (size_t j=0; j<expanded.size(); j++) {
const FunctionType *eft = expanded[j]; const FunctionType *eft = expanded[j];
@@ -1060,7 +1059,14 @@ Module::AddFunctionDeclaration(const std::string &name,
nargsp.push_back(eft->GetParameterSourcePos(k)); nargsp.push_back(eft->GetParameterSourcePos(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, nargs,
nargsn, nargsn,
nargsd, nargsd,
@@ -1078,6 +1084,11 @@ Module::AddFunctionDeclaration(const std::string &name,
if (expanded.size() > 1) { if (expanded.size() > 1) {
for (size_t i=0; i<expanded.size(); i++) { for (size_t i=0; i<expanded.size(); i++) {
if (expanded[i]->GetReturnType()->IsPolymorphicType()) {
Error(pos, "Unexpected polymorphic return type \"%s\"",
expanded[i]->GetReturnType()->GetString().c_str());
return;
}
std::string nname = name; std::string nname = name;
if (functionType->isExported || functionType->isExternC) { if (functionType->isExported || functionType->isExternC) {
for (int j=0; j<expanded[i]->GetNumParameters(); j++) { for (int j=0; j<expanded[i]->GetNumParameters(); j++) {

View File

@@ -147,7 +147,7 @@ bool
SymbolTable::AddFunction(Symbol *symbol) { SymbolTable::AddFunction(Symbol *symbol) {
const FunctionType *ft = CastType<FunctionType>(symbol->type); const FunctionType *ft = CastType<FunctionType>(symbol->type);
Assert(ft != NULL); 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 // A function of the same name and type has already been added to
// the symbol table // the symbol table
return false; return false;
@@ -183,7 +183,8 @@ SymbolTable::LookupFunction(const char *name, std::vector<Symbol *> *matches) {
Symbol * 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); FunctionMapType::iterator iter = functions.find(name);
if (iter != functions.end()) { if (iter != functions.end()) {
std::vector<Symbol *> funcs = iter->second; std::vector<Symbol *> funcs = iter->second;
@@ -193,7 +194,7 @@ SymbolTable::LookupFunction(const char *name, const FunctionType *type) {
} }
} }
// Try looking for a polymorphic function // Try looking for a polymorphic function
if (polyFunctions[name].size() > 0) { if (!ignorePoly && polyFunctions[name].size() > 0) {
std::string n = name; std::string n = name;
return new Symbol(name, polyFunctions[name][0]->pos, type); return new Symbol(name, polyFunctions[name][0]->pos, type);
} }

3
sym.h
View File

@@ -181,7 +181,8 @@ public:
in the symbol table. in the symbol table.
@return pointer to matching Symbol; NULL if none is found. */ @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<Symbol *>& LookupPolyFunction(const char *name); std::vector<Symbol *>& LookupPolyFunction(const char *name);

View File

@@ -4078,7 +4078,8 @@ bool
Type::IsBasicType(const Type *type) { Type::IsBasicType(const Type *type) {
return (CastType<AtomicType>(type) != NULL || return (CastType<AtomicType>(type) != NULL ||
CastType<EnumType>(type) != NULL || CastType<EnumType>(type) != NULL ||
CastType<PointerType>(type) != NULL); CastType<PointerType>(type) != NULL ||
CastType<PolyType>(type) != NULL);
} }