X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/be56e29997e17685eebf8bd7cb745183c60de7db..17860defa84c6d8bc0e8bc088a7e09361f17db07:/src/typeInfo.h diff --git a/src/typeInfo.h b/src/typeInfo.h index 3914847..89fff2a 100644 --- a/src/typeInfo.h +++ b/src/typeInfo.h @@ -15,7 +15,8 @@ TypeInfo typeType(const Program & p, Type t) TypeInfo result; result.isStruct = true; if (t.name == "int" || t.name == "float" || t.name == "double" || - t.name == "char" || t.name == "long" || t.name == "short" || t.name == "bool") + t.name == "char" || t.name == "long" || t.name == "short" || t.name == "bool" || + t.name == "void") { result.isStruct = false; } @@ -23,7 +24,7 @@ TypeInfo typeType(const Program & p, Type t) return result; } -TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace, Expr e) +TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace, std::shared_ptr globalCtx, Expr e) { TypeInfo result; @@ -35,7 +36,7 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace namespacePrefixes.insert(namespacePrefixes.end(), e._func.namespacePrefixes.begin(), e._func.namespacePrefixes.end()); - auto f = findFunction(p, e._func.functionName, namespacePrefixes); + auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx); if (!f.has_value()) throw "Unknown function"; result = typeType(p, f.value().returnType); @@ -43,11 +44,16 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace } case ExprType::Method: { - TypeInfo tiCaller = typeExpr(p, globalNamespace, *e._method.expr); - auto m = findStructMethod(p, e._method.methodName, tiCaller); + TypeInfo tiCaller = typeExpr(p, globalNamespace, globalCtx, *e._method.expr); + if (!tiCaller.isStruct) + throw "Calling method on non-struct"; + auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx); + if (!s.has_value()) + throw "Calling method on unknown struct"; + auto m = findStructMethod(e._method.methodName, s.value()); if (!m.has_value()) throw "Unknown method"; - result = typeType(p, m.value().returnType); + result = typeType(p, m.value().t.returnType); break; } case ExprType::Lit: @@ -61,32 +67,37 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace } break; case ExprType::Paren: - result = typeExpr(p, globalNamespace, *e._paren.expr); + result = typeExpr(p, globalNamespace, globalCtx, *e._paren.expr); break; case ExprType::Dot: { - auto sm = findStructMember(p, - typeExpr(p, globalNamespace, *e._dot.expr), e._dot.identifier); + auto tiCaller = typeExpr(p, globalNamespace, globalCtx, *e._dot.expr); + if (!tiCaller.isStruct) + throw "Accessing member of non-struct"; + auto s = findStruct(tiCaller.type.name, tiCaller.type.namespacePrefixes, globalCtx); + if (!s.has_value()) + throw "Calling method on unknown struct"; + auto sm = findStructMember(e._dot.identifier, s.value()); if (!sm.has_value()) throw "Unknown struct member"; - result = typeType(p, sm.value().type); + result = typeType(p, sm.value().t.type); break; } case ExprType::PrefixOp: - result = typeExpr(p, globalNamespace, *e._prefixOp.expr); + result = typeExpr(p, globalNamespace, globalCtx, *e._prefixOp.expr); break; case ExprType::PostfixOp: - result = typeExpr(p, globalNamespace, *e._postfixOp.expr); + result = typeExpr(p, globalNamespace, globalCtx, *e._postfixOp.expr); break; case ExprType::BinaryOp: - result = typeExpr(p, globalNamespace, *e._binaryOp.lexpr); + result = typeExpr(p, globalNamespace, globalCtx, *e._binaryOp.lexpr); break; case ExprType::TernaryOp: - result = typeExpr(p, globalNamespace, *e._ternaryOp.rexprTrue); + result = typeExpr(p, globalNamespace, globalCtx, *e._ternaryOp.rexprTrue); break; case ExprType::Bracket: { - TypeInfo ti = typeExpr(p, globalNamespace, *e._brackets.lexpr); + TypeInfo ti = typeExpr(p, globalNamespace, globalCtx, *e._brackets.lexpr); if (!ti.type.modifiers.empty()) { result = ti; @@ -103,7 +114,7 @@ TypeInfo typeExpr(const Program & p, const std::vector & globalNamespace namespacePrefixes.insert(namespacePrefixes.end(), e._identifier.namespacePrefixes.begin(), e._identifier.namespacePrefixes.end()); - auto v = findVariable(p, e._identifier.identifier, namespacePrefixes); + auto v = findVariable(e._identifier.identifier, namespacePrefixes, globalCtx); if (!v.has_value()) throw "Unknown variable"; result = typeType(p, v.value().type);