aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/ControlFlowGen.cc8
-rw-r--r--lib/CodeGen/ExpressionGen.cc11
-rw-r--r--lib/CodeGen/FunctionGen.cc23
-rw-r--r--lib/CodeGen/OperatorGen.cc20
-rw-r--r--lib/CodeGen/TypeCheckGen.cc4
-rw-r--r--lib/CodeGen/ValueTypeGen.cc8
6 files changed, 72 insertions, 2 deletions
diff --git a/lib/CodeGen/ControlFlowGen.cc b/lib/CodeGen/ControlFlowGen.cc
index 17f43f2..0cf0c34 100644
--- a/lib/CodeGen/ControlFlowGen.cc
+++ b/lib/CodeGen/ControlFlowGen.cc
@@ -204,6 +204,14 @@ void CodeGen::emitCaseStmt(CaseStmtAST *Stmt) {
return nullptr;
}
Value *LabelValue = emitExpr(Label);
+ auto Overload = resolveOperator(Op, SelectorType, LabelType);
+ if (Overload.Signature && !Overload.Ambiguous) {
+ const TypeInfo *Result = resolveType(Overload.Signature->ReturnTypeName);
+ if (!Result || Result->Kind != TypeKind::Boolean) {
+ reportError("CASE comparison must return BOOLEAN");
+ return nullptr;
+ }
+ }
return emitBinaryOp(Op, Selected, SelectorType, LabelValue, LabelType, Branch.Line);
};
if (!Branch.Lower) {
diff --git a/lib/CodeGen/ExpressionGen.cc b/lib/CodeGen/ExpressionGen.cc
index 79c5fb0..8fb13c9 100644
--- a/lib/CodeGen/ExpressionGen.cc
+++ b/lib/CodeGen/ExpressionGen.cc
@@ -4,6 +4,7 @@
#include "cps/MemoryHandler.h"
#include "cps/Lexer.h"
#include "cps/TypeAST.h"
+#include "cps/Operator.h"
#include "Builtins.h"
#include <climits>
#include <cstddef>
@@ -155,6 +156,16 @@ Value *CodeGen::emitBinaryOp(int Op, Value *L, const TypeInfo *LT,
Value *R, const TypeInfo *RT, int Line) {
SaveAndRestore<int> Location(DiagnosticLine, Line ? Line : DiagnosticLine);
if (!L || !R) return nullptr;
+ if (!LT || !RT) {
+ reportError("Cannot determine operand types for %s", operatorName(Op).c_str());
+ return nullptr;
+ }
+ auto Overload = resolveOperator(Op, LT, RT);
+ if (Overload.Ambiguous) {
+ reportError("Ambiguous overload for %s(%s, %s)", operatorName(Op).c_str(), LT->Name.c_str(), RT->Name.c_str());
+ return nullptr;
+ }
+ if (Overload.Signature) return FuncGen->emitCall(*Overload.Signature, {L, R});
if ((LT && LT->isC()) || (RT && RT->isC()))
return emitCBinaryOp(Op, L, LT, R, RT, Line);
if ((LT && LT->isUserKind()) || (RT && RT->isUserKind()))
diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc
index 9559717..bccf397 100644
--- a/lib/CodeGen/FunctionGen.cc
+++ b/lib/CodeGen/FunctionGen.cc
@@ -2,6 +2,7 @@
#include "cps/FunctionGen.h"
#include "cps/CodeGen.h"
#include "cps/ExceptionHandler.h"
+#include "cps/Operator.h"
#include <cstdio>
#include <cstdarg>
@@ -122,6 +123,28 @@ const FuncSig *FunctionGen::getSignatureByLink(const std::string &Name) const {
Function *FunctionGen::emitPrototype(PrototypeAST *Proto) {
auto Registered = PrototypeLinks.find(Proto);
if (Registered != PrototypeLinks.end()) return Module.getFunction(Registered->second);
+ if (int Op = Proto->getOperator()) {
+ size_t Count = Proto->getParams().size() + !Proto->getOwnerClass().empty();
+ bool ValidCount = Op == '-' ? Count == 1 || Count == 2
+ : Op == tok_not ? Count == 1 : Count == 2;
+ if (!ValidCount) {
+ reportError("Invalid operand count for %s", Proto->getName().c_str());
+ return nullptr;
+ }
+ bool HasUserOperand = !Proto->getOwnerClass().empty();
+ for (const auto &P : Proto->getParams()) {
+ if (P.IsArray) {
+ reportError("An operator parameter cannot be a whole array");
+ return nullptr;
+ }
+ const TypeInfo *T = Types.resolve(P.TypeName);
+ HasUserOperand |= T && (T->isRecord() || T->isEnum());
+ }
+ if (!HasUserOperand) {
+ reportError("%s requires a record, class or enum operand", Proto->getName().c_str());
+ return nullptr;
+ }
+ }
if (Proto->isOverload() && Proto->getOwnerClass().empty() && sameIdentifier(Proto->getName(), "MAIN")) {
reportError("MAIN cannot be declared OVERLOAD");
return nullptr;
diff --git a/lib/CodeGen/OperatorGen.cc b/lib/CodeGen/OperatorGen.cc
new file mode 100644
index 0000000..83c515b
--- /dev/null
+++ b/lib/CodeGen/OperatorGen.cc
@@ -0,0 +1,20 @@
+#include "cps/CodeGen.h"
+#include "cps/Operator.h"
+
+using namespace cps;
+
+// Probing candidates must not evaluate operands or diagnose absent overloads:
+// both static typing and emission use this same selection.
+CodeGen::OperatorResolution CodeGen::resolveOperator(
+ int Op, const TypeInfo *Left, const TypeInfo *Right) {
+ OperatorResolution Result;
+ if (!Left) return Result;
+ for (const FuncSig *Sig : FuncGen->getOverloads(operatorName(Op))) {
+ if (Sig->Prototype->getOperator() != Op || Sig->Params.size() != (Right ? 2u : 1u)) continue;
+ if (!Left->sameType(resolveType(Sig->Params[0].TypeName))) continue;
+ if (Right && !Right->sameType(resolveType(Sig->Params[1].TypeName))) continue;
+ if (Result.Signature) Result.Ambiguous = true;
+ else Result.Signature = Sig;
+ }
+ return Result;
+}
diff --git a/lib/CodeGen/TypeCheckGen.cc b/lib/CodeGen/TypeCheckGen.cc
index 690d005..5d014f6 100644
--- a/lib/CodeGen/TypeCheckGen.cc
+++ b/lib/CodeGen/TypeCheckGen.cc
@@ -46,6 +46,10 @@ Value *CodeGen::emitCoercedExpr(ExprAST *E, const TypeInfo *Target) {
}
const TypeInfo *Src = getExprTypeInfo(E);
+ if (Src && Src->Kind == TypeKind::String && Target->Kind == TypeKind::Boolean) {
+ reportError("Cannot implicitly convert STRING to BOOLEAN");
+ return nullptr;
+ }
if (Target->isC() || (Src && Src->isC())) {
if (auto *Addr = dynamic_cast<AddrOfExprAST*>(E)) {
diff --git a/lib/CodeGen/ValueTypeGen.cc b/lib/CodeGen/ValueTypeGen.cc
index 3c1fc06..b09a5d9 100644
--- a/lib/CodeGen/ValueTypeGen.cc
+++ b/lib/CodeGen/ValueTypeGen.cc
@@ -81,6 +81,12 @@ const TypeInfo *CodeGen::getExprTypeInfo(ExprAST *Expr) {
}
if (auto *Bin = dynamic_cast<BinaryExprAST*>(Expr)) {
+ const TypeInfo *L = getExprTypeInfo(Bin->getLHS());
+ const TypeInfo *R = getExprTypeInfo(Bin->getRHS());
+ if (!L || !R || isWholeArrayVar(Bin->getLHS()) || isWholeArrayVar(Bin->getRHS())) return nullptr;
+ auto Overload = resolveOperator(Bin->getOp(), L, R);
+ if (Overload.Ambiguous) return nullptr;
+ if (Overload.Signature) return resolveType(Overload.Signature->ReturnTypeName);
switch (Bin->getOp()) {
case tok_eq:
case tok_ne:
@@ -99,8 +105,6 @@ const TypeInfo *CodeGen::getExprTypeInfo(ExprAST *Expr) {
break;
}
- const TypeInfo *L = getExprTypeInfo(Bin->getLHS());
- const TypeInfo *R = getExprTypeInfo(Bin->getRHS());
if ((L && L->isC()) || (R && R->isC())) return Types->commonCScalar(L, R);
// Enum arithmetic: E +/- INTEGER (enum on the left) yields the enum;
// every other user-kind combination has no type (emission rejects it).