Compiler errors
This commit is contained in:
@@ -15,9 +15,6 @@ uses
|
||||
Myc.Ast;
|
||||
|
||||
type
|
||||
// Exception for type mismatches during compilation
|
||||
ETypeCheckException = class(EAstException);
|
||||
|
||||
IAstTypeChecker = interface(IAstVisitor)
|
||||
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||
end;
|
||||
@@ -40,6 +37,8 @@ type
|
||||
|
||||
private
|
||||
FCurrentContext: TTypeContext;
|
||||
FLog: ICompilerLog;
|
||||
|
||||
function CreateContextChain(L: IScopeLayout): TTypeContext;
|
||||
|
||||
protected
|
||||
@@ -64,12 +63,12 @@ type
|
||||
function VisitKeyword(const Node: IKeywordNode): IAstNode; override;
|
||||
|
||||
public
|
||||
constructor Create(const RootLayout: IScopeLayout);
|
||||
constructor Create(const RootLayout: IScopeLayout; const ALog: ICompilerLog);
|
||||
destructor Destroy; override;
|
||||
|
||||
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||
|
||||
class function CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode; static;
|
||||
class function CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout; const ALog: ICompilerLog): IAstNode; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -107,8 +106,11 @@ begin
|
||||
for i := 1 to Address.ScopeDepth do
|
||||
begin
|
||||
if not Assigned(ctx.FParent) then
|
||||
raise ETypeCheckException
|
||||
.CreateFmt('Scope depth mismatch during type lookup. Requested Depth: %d.', [Address.ScopeDepth]);
|
||||
begin
|
||||
// Should technically not happen if Binder did its job, but safe guard it.
|
||||
Result := TTypes.Unknown;
|
||||
Exit;
|
||||
end;
|
||||
ctx := ctx.FParent;
|
||||
end;
|
||||
|
||||
@@ -148,9 +150,10 @@ begin
|
||||
Result := TTypeContext.Create(p, L, []);
|
||||
end;
|
||||
|
||||
constructor TTypeChecker.Create(const RootLayout: IScopeLayout);
|
||||
constructor TTypeChecker.Create(const RootLayout: IScopeLayout; const ALog: ICompilerLog);
|
||||
begin
|
||||
inherited Create;
|
||||
FLog := ALog;
|
||||
FCurrentContext := CreateContextChain(RootLayout);
|
||||
if FCurrentContext = nil then
|
||||
FCurrentContext := TTypeContext.Create(nil, nil, []);
|
||||
@@ -167,9 +170,9 @@ begin
|
||||
inherited;
|
||||
end;
|
||||
|
||||
class function TTypeChecker.CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||
class function TTypeChecker.CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout; const ALog: ICompilerLog): IAstNode;
|
||||
begin
|
||||
var checker := TTypeChecker.Create(Layout) as IAstTypeChecker;
|
||||
var checker := TTypeChecker.Create(Layout, ALog) as IAstTypeChecker;
|
||||
Result := checker.Execute(RootNode, Layout);
|
||||
end;
|
||||
|
||||
@@ -182,13 +185,11 @@ end;
|
||||
|
||||
function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode;
|
||||
begin
|
||||
Assert(Node.StaticType.Kind <> stUnknown);
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode;
|
||||
begin
|
||||
Assert(Node.StaticType.Kind = stKeyword);
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
@@ -198,6 +199,14 @@ var
|
||||
adr: TResolvedAddress;
|
||||
begin
|
||||
adr := Node.Address;
|
||||
|
||||
// If binder failed to resolve, we propagate Unknown type to prevent cascading errors.
|
||||
if adr.Kind = akUnresolved then
|
||||
begin
|
||||
Result := TAst.Identifier(Node.Name, adr, TTypes.Unknown);
|
||||
Exit;
|
||||
end;
|
||||
|
||||
typ := FCurrentContext.LookupType(adr);
|
||||
Result := TAst.Identifier(Node.Name, adr, typ);
|
||||
end;
|
||||
@@ -226,6 +235,15 @@ begin
|
||||
adr := Node.Target.AsIdentifier.Address;
|
||||
initType := TTypes.Unknown;
|
||||
|
||||
// If address is unresolved (binder error), we just process the initializer and return.
|
||||
if adr.Kind = akUnresolved then
|
||||
begin
|
||||
if Assigned(Node.Initializer) then
|
||||
Accept(Node.Initializer);
|
||||
Result := Node;
|
||||
Exit;
|
||||
end;
|
||||
|
||||
placeholderType := nil;
|
||||
if (Node.Initializer <> nil) and (Node.Initializer.Kind = akLambdaExpression) then
|
||||
begin
|
||||
@@ -270,6 +288,14 @@ begin
|
||||
targetType := newIdent.AsTypedNode.StaticType;
|
||||
adr := newIdent.AsIdentifier.Address;
|
||||
|
||||
// If binder failed, address is unresolved. We still check the value but skip assignment logic.
|
||||
if adr.Kind = akUnresolved then
|
||||
begin
|
||||
Accept(Node.Value);
|
||||
Result := Node;
|
||||
Exit;
|
||||
end;
|
||||
|
||||
placeholderType := nil;
|
||||
if (Node.Value <> nil) and (Node.Value.Kind = akLambdaExpression) then
|
||||
begin
|
||||
@@ -290,17 +316,18 @@ begin
|
||||
newValue := Accept(Node.Value);
|
||||
sourceType := newValue.AsTypedNode.StaticType;
|
||||
|
||||
if not TTypeRules.CanAssign(targetType, sourceType) then
|
||||
// Only check assignment if both types are known to avoid cascading error reports
|
||||
if (targetType.Kind <> stUnknown) and (sourceType.Kind <> stUnknown) then
|
||||
begin
|
||||
if (targetType.Kind = stUnknown) then
|
||||
if not TTypeRules.CanAssign(targetType, sourceType) then
|
||||
begin
|
||||
if not TTypeRules.CanAssign(sourceType, targetType) then
|
||||
raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
|
||||
end
|
||||
else
|
||||
raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]), Node);
|
||||
// We continue, effectively ignoring the assignment type-wise (variable keeps old type).
|
||||
end;
|
||||
end;
|
||||
|
||||
// Type Inference for 'Unknown' targets
|
||||
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
|
||||
begin
|
||||
FCurrentContext.SetType(adr.SlotIndex, sourceType);
|
||||
@@ -337,14 +364,20 @@ begin
|
||||
var paramIdent := Node.Parameters[i];
|
||||
var paramAdr := paramIdent.Address;
|
||||
paramTypes[i] := TTypes.Unknown;
|
||||
FCurrentContext.SetType(paramAdr.SlotIndex, paramTypes[i]);
|
||||
|
||||
if paramAdr.Kind <> akUnresolved then
|
||||
FCurrentContext.SetType(paramAdr.SlotIndex, paramTypes[i]);
|
||||
|
||||
newParams[i] := TAst.Identifier(paramIdent.Name, paramAdr, paramTypes[i]);
|
||||
end;
|
||||
|
||||
newBody := Accept(Node.Body);
|
||||
bodyType := newBody.AsTypedNode.StaticType;
|
||||
methodType := TTypes.CreateMethod(paramTypes, bodyType);
|
||||
|
||||
// Set self-reference type (slot 0 is <self>)
|
||||
FCurrentContext.SetType(0, methodType);
|
||||
|
||||
finalDescriptor := TScope.CreateDescriptor(Node.Layout, FCurrentContext.Types);
|
||||
finally
|
||||
var temp := FCurrentContext;
|
||||
@@ -367,6 +400,7 @@ var
|
||||
bestSig: IMethodSignature;
|
||||
sig: IMethodSignature;
|
||||
match: Boolean;
|
||||
argsStr: string;
|
||||
begin
|
||||
newCallee := Accept(Node.Callee);
|
||||
SetLength(newArgs, Length(Node.Arguments));
|
||||
@@ -415,16 +449,34 @@ begin
|
||||
retType := bestSig.ReturnType
|
||||
else
|
||||
begin
|
||||
var argsStr: string := '';
|
||||
for i := 0 to High(argTypes) do
|
||||
argsStr := argsStr + argTypes[i].ToString + ' ';
|
||||
raise ETypeCheckException
|
||||
.CreateFmt('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]);
|
||||
// Log error, but don't crash
|
||||
if Assigned(FLog) then
|
||||
begin
|
||||
argsStr := '';
|
||||
for i := 0 to High(argTypes) do
|
||||
argsStr := argsStr + argTypes[i].ToString + ' ';
|
||||
FLog.AddError(
|
||||
Format('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]),
|
||||
Node
|
||||
);
|
||||
end;
|
||||
retType := TTypes.Unknown; // Poison result
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
// We have unknown arguments (probably due to upstream errors).
|
||||
// We cannot resolve the signature, so the result is Unknown.
|
||||
// We do NOT log an error here to avoid spam.
|
||||
retType := TTypes.Unknown;
|
||||
end;
|
||||
end
|
||||
else if calleeType.Kind <> TStaticTypeKind.stUnknown then
|
||||
raise ETypeCheckException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot invoke type %s as a function.', [calleeType.ToString]), Node);
|
||||
retType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
Result := TAst.FunctionCall(newCallee, newArgs, retType, Node.IsTailCall);
|
||||
end;
|
||||
@@ -457,10 +509,14 @@ begin
|
||||
newElse := Accept(Node.ElseBranch);
|
||||
|
||||
conditionType := newCond.AsTypedNode.StaticType;
|
||||
|
||||
if (conditionType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
||||
raise ETypeCheckException.CreateFmt('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]), Node);
|
||||
end;
|
||||
|
||||
thenType := newThen.AsTypedNode.StaticType;
|
||||
elseType :=
|
||||
@@ -469,7 +525,11 @@ begin
|
||||
|
||||
resultType := TTypeRules.Promote(thenType, elseType);
|
||||
if resultType = nil then
|
||||
raise ETypeCheckException.CreateFmt('Cannot promote types %s and %s in If-Expression', [thenType.ToString, elseType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot promote types %s and %s in If-Expression', [thenType.ToString, elseType.ToString]), Node);
|
||||
resultType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
Result := TAst.IfExpr(newCond, newThen, newElse, resultType);
|
||||
end;
|
||||
@@ -487,14 +547,21 @@ begin
|
||||
if (conditionType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
||||
raise ETypeCheckException.CreateFmt('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]), Node);
|
||||
end;
|
||||
|
||||
thenType := newThen.AsTypedNode.StaticType;
|
||||
elseType := newElse.AsTypedNode.StaticType;
|
||||
|
||||
resultType := TTypeRules.Promote(thenType, elseType);
|
||||
if resultType = nil then
|
||||
raise ETypeCheckException.CreateFmt('Cannot promote types %s and %s in Ternary-Expression', [thenType.ToString, elseType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot promote types %s and %s in Ternary-Expression', [thenType.ToString, elseType.ToString]), Node);
|
||||
resultType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
Result := TAst.TernaryExpr(newCond, newThen, newElse, resultType);
|
||||
end;
|
||||
@@ -517,25 +584,36 @@ begin
|
||||
begin
|
||||
fieldIndex := baseType.Definition.IndexOf(Node.Member.Value);
|
||||
if fieldIndex < 0 then
|
||||
raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
|
||||
|
||||
var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value);
|
||||
|
||||
if baseType.Kind = TStaticTypeKind.stRecord then
|
||||
elemType := fieldType
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
elemType := TTypes.CreateSeries(fieldType);
|
||||
begin
|
||||
var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value);
|
||||
if baseType.Kind = TStaticTypeKind.stRecord then
|
||||
elemType := fieldType
|
||||
else
|
||||
elemType := TTypes.CreateSeries(fieldType);
|
||||
end;
|
||||
end
|
||||
else if (baseType.Kind = TStaticTypeKind.stGenericRecord) then
|
||||
begin
|
||||
var genDef := baseType.GenericDefinition;
|
||||
fieldIndex := genDef.IndexOf(Node.Member.Value);
|
||||
if fieldIndex < 0 then
|
||||
raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
|
||||
elemType := genDef.Fields[fieldIndex].Value;
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
elemType := genDef.Fields[fieldIndex].Value;
|
||||
end
|
||||
else
|
||||
raise ETypeCheckException.CreateFmt('Member access requires a record type, but got %s', [baseType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member access requires a record type, but got %s', [baseType.ToString]), Node);
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType);
|
||||
@@ -556,15 +634,23 @@ begin
|
||||
if (baseType.Kind <> TStaticTypeKind.stUnknown) then
|
||||
begin
|
||||
if (baseType.Kind <> TStaticTypeKind.stSeries) and (baseType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||
raise ETypeCheckException.CreateFmt('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]);
|
||||
|
||||
if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then
|
||||
raise ETypeCheckException.CreateFmt('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]);
|
||||
|
||||
if baseType.Kind = TStaticTypeKind.stSeries then
|
||||
elemType := baseType.ElementType
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
elemType := TTypes.CreateRecord(baseType.Definition);
|
||||
begin
|
||||
if baseType.Kind = TStaticTypeKind.stSeries then
|
||||
elemType := baseType.ElementType
|
||||
else
|
||||
elemType := TTypes.CreateRecord(baseType.Definition);
|
||||
end;
|
||||
end;
|
||||
|
||||
if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]), Node);
|
||||
end;
|
||||
|
||||
Result := TAst.Indexer(newBase, newIndex, elemType);
|
||||
@@ -639,12 +725,14 @@ var
|
||||
elemType: IStaticType;
|
||||
begin
|
||||
try
|
||||
// TScalar.StringToKind might throw EConvertError or generic Exception if string is invalid
|
||||
elemType := TTypes.FromScalarKind(TScalar.StringToKind(Node.Definition));
|
||||
except
|
||||
on E: Exception do
|
||||
// Instead of silently using Unknown, we now raise a proper compiler error
|
||||
raise ETypeCheckException.CreateFmt('Invalid type definition "%s" in new-series.', [Node.Definition]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Invalid type definition "%s" in new-series: %s', [Node.Definition, E.Message]), Node);
|
||||
elemType := TTypes.Unknown;
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := TAst.CreateSeries(Node.Definition, TTypes.CreateSeries(elemType));
|
||||
@@ -665,18 +753,32 @@ begin
|
||||
if (seriesType.Kind <> stUnknown) then
|
||||
begin
|
||||
if (seriesType.Kind <> TStaticTypeKind.stSeries) then
|
||||
raise ETypeCheckException.CreateFmt('"add" requires a series as its first argument, but got %s', [seriesType.ToString]);
|
||||
|
||||
if not TTypeRules.CanAssign(seriesType.ElementType, valueType) then
|
||||
raise ETypeCheckException
|
||||
.CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('"add" requires a series as its first argument, but got %s', [seriesType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
begin
|
||||
// Only check assignment compatibility if value type is known
|
||||
if (valueType.Kind <> stUnknown) and not TTypeRules.CanAssign(seriesType.ElementType, valueType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]),
|
||||
Node
|
||||
);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
if (newLookback <> nil) then
|
||||
begin
|
||||
var lookbackType := newLookback.AsTypedNode.StaticType;
|
||||
if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then
|
||||
raise ETypeCheckException.Create('Lookback parameter for "add" must be an ordinal value.');
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError('Lookback parameter for "add" must be an ordinal value.', Node);
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
|
||||
@@ -698,7 +800,10 @@ begin
|
||||
if (seriesType.Kind <> stUnknown)
|
||||
and (seriesType.Kind <> TStaticTypeKind.stSeries)
|
||||
and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||
raise ETypeCheckException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]);
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('"length" requires a series, but got %s', [seriesType.ToString]), Node);
|
||||
end;
|
||||
|
||||
Result := TAst.SeriesLength(newSeries.AsIdentifier, TTypes.Ordinal);
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user