refactoring and reducing complexity
This commit is contained in:
+40
-56
@@ -1,7 +1,9 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.diagnostics.Diagnostics;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.ExprUse;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.Kind;
|
||||
@@ -20,10 +22,6 @@ final class PbsFlowBodyAnalyzer {
|
||||
boolean assignable) {
|
||||
}
|
||||
|
||||
public void validate(final PbsAst.File ast, final DiagnosticSink diagnostics) {
|
||||
validate(ast, ReadOnlyList.empty(), diagnostics);
|
||||
}
|
||||
|
||||
public void validate(
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
@@ -118,14 +116,14 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
|
||||
if (enforceResultCompletion) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_POSSIBLE_FALLTHROUGH_RESULT.name(),
|
||||
"Possible fallthrough in result callable body; all paths must return explicitly",
|
||||
body.span());
|
||||
return;
|
||||
}
|
||||
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_POSSIBLE_FALLTHROUGH_NON_UNIT.name(),
|
||||
"Possible fallthrough in plain non-unit callable body; all paths must return explicitly",
|
||||
body.span());
|
||||
@@ -171,26 +169,15 @@ final class PbsFlowBodyAnalyzer {
|
||||
private boolean expressionAlwaysReturns(
|
||||
final PbsAst.Expression expression,
|
||||
final Model model) {
|
||||
if (expression == null) {
|
||||
return false;
|
||||
}
|
||||
if (expression instanceof PbsAst.GroupExpr groupExpr) {
|
||||
return expressionAlwaysReturns(groupExpr.expression(), model);
|
||||
}
|
||||
if (expression instanceof PbsAst.BlockExpr blockExpr) {
|
||||
return blockAlwaysReturns(blockExpr.block(), model);
|
||||
}
|
||||
if (expression instanceof PbsAst.IfExpr ifExpr) {
|
||||
return blockAlwaysReturns(ifExpr.thenBlock(), model)
|
||||
return switch (expression) {
|
||||
case PbsAst.GroupExpr groupExpr -> expressionAlwaysReturns(groupExpr.expression(), model);
|
||||
case PbsAst.BlockExpr blockExpr -> blockAlwaysReturns(blockExpr.block(), model);
|
||||
case PbsAst.IfExpr ifExpr -> blockAlwaysReturns(ifExpr.thenBlock(), model)
|
||||
&& expressionAlwaysReturns(ifExpr.elseExpression(), model);
|
||||
}
|
||||
if (expression instanceof PbsAst.SwitchExpr switchExpr) {
|
||||
return switchAlwaysReturns(switchExpr, model);
|
||||
}
|
||||
if (expression instanceof PbsAst.HandleExpr handleExpr) {
|
||||
return handleAlwaysReturns(handleExpr, model);
|
||||
}
|
||||
return false;
|
||||
case PbsAst.SwitchExpr switchExpr -> switchAlwaysReturns(switchExpr, model);
|
||||
case PbsAst.HandleExpr handleExpr -> handleAlwaysReturns(handleExpr, model);
|
||||
case null, default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean handleAlwaysReturns(
|
||||
@@ -373,7 +360,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
if (!typeOps.isBool(condition)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_IF_NON_BOOL_CONDITION.name(),
|
||||
"If statement condition must have bool type",
|
||||
ifStatement.condition().span());
|
||||
@@ -390,7 +377,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
if (statement instanceof PbsAst.ForStatement(
|
||||
String iteratorName, PbsAst.TypeRef type, PbsAst.Expression fromExpression,
|
||||
PbsAst.Expression untilExpression, PbsAst.Expression stepExpression, PbsAst.Block body,
|
||||
p.studio.compiler.source.Span span
|
||||
Span span
|
||||
)) {
|
||||
final var iteratorType = typeOps.typeFromTypeRef(type, model, receiverType);
|
||||
final var fromType = expressionAnalyzer.analyzeExpression(
|
||||
@@ -418,7 +405,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
if (!typeOps.compatible(fromType, iteratorType) || !typeOps.compatible(untilType, iteratorType)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_FOR_TYPE_MISMATCH.name(),
|
||||
"For-loop bounds must match declared iterator type",
|
||||
span);
|
||||
@@ -437,7 +424,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
if (!typeOps.compatible(stepType, iteratorType)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_FOR_TYPE_MISMATCH.name(),
|
||||
"For-loop step must match declared iterator type",
|
||||
stepExpression.span());
|
||||
@@ -462,7 +449,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
if (!typeOps.isBool(condition)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_WHILE_NON_BOOL_CONDITION.name(),
|
||||
"While condition must have bool type",
|
||||
whileStatement.condition().span());
|
||||
@@ -529,8 +516,8 @@ final class PbsFlowBodyAnalyzer {
|
||||
final TypeView receiverType,
|
||||
final Model model,
|
||||
final DiagnosticSink diagnostics) {
|
||||
if (returnType.kind() != PbsFlowSemanticSupport.Kind.RESULT || resultErrorName == null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
if (returnType.kind() != Kind.RESULT || resultErrorName == null) {
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_RESULT_FLOW_INVALID_POSITION.name(),
|
||||
"'ok(...)' is only allowed when returning from a result callable",
|
||||
okExpr.span());
|
||||
@@ -563,7 +550,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
if (!typeOps.compatible(actualType, payloadType)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_RESULT_OK_PAYLOAD_MISMATCH.name(),
|
||||
"Payload in 'ok(...)' is incompatible with result payload type",
|
||||
okExpr.value().span());
|
||||
@@ -576,8 +563,8 @@ final class PbsFlowBodyAnalyzer {
|
||||
final String resultErrorName,
|
||||
final Model model,
|
||||
final DiagnosticSink diagnostics) {
|
||||
if (returnType.kind() != PbsFlowSemanticSupport.Kind.RESULT || resultErrorName == null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
if (returnType.kind() != Kind.RESULT || resultErrorName == null) {
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_RESULT_FLOW_INVALID_POSITION.name(),
|
||||
"'err(...)' is only allowed when returning from a result callable",
|
||||
errExpr.span());
|
||||
@@ -585,7 +572,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
|
||||
if (!matchesTargetError(errExpr.errorPath(), resultErrorName, model)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_RESULT_ERROR_LABEL_INVALID.name(),
|
||||
"Error label in 'err(...)' does not match enclosing result error type",
|
||||
errExpr.errorPath().span());
|
||||
@@ -621,7 +608,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
final Model model,
|
||||
final DiagnosticSink diagnostics) {
|
||||
final var target = resolveAssignmentTarget(assignStatement.target(), scope, receiverType, model, diagnostics);
|
||||
final var expectedType = target == null ? null : target.type();
|
||||
final var expectedType = target.type();
|
||||
final var valueType = expressionAnalyzer.analyzeExpression(
|
||||
assignStatement.value(),
|
||||
scope,
|
||||
@@ -635,12 +622,12 @@ final class PbsFlowBodyAnalyzer {
|
||||
true,
|
||||
this::analyzeBlock).type();
|
||||
|
||||
if (target == null || !target.assignable()) {
|
||||
if (!target.assignable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!assignmentCompatible(assignStatement.operator(), target.type(), valueType)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TYPE_MISMATCH.name(),
|
||||
"Assigned value type is not compatible with assignment target",
|
||||
assignStatement.span());
|
||||
@@ -658,12 +645,12 @@ final class PbsFlowBodyAnalyzer {
|
||||
if (lValue.pathSegments().isEmpty()) {
|
||||
if (rootIsThis) {
|
||||
if (receiverType == null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_INVALID_THIS_CONTEXT.name(),
|
||||
"Invalid 'this' usage outside struct/service methods and constructors",
|
||||
lValue.span());
|
||||
}
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_NOT_ASSIGNABLE.name(),
|
||||
"Assignment target is not assignable",
|
||||
lValue.span());
|
||||
@@ -673,7 +660,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
final var localType = scope.resolve(rootName);
|
||||
if (localType != null) {
|
||||
if (!scope.isMutable(rootName)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_NOT_ASSIGNABLE.name(),
|
||||
"Cannot assign to immutable local '%s'".formatted(rootName),
|
||||
lValue.span());
|
||||
@@ -683,14 +670,14 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
|
||||
if (isKnownNonAssignableRoot(rootName, model)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_NOT_ASSIGNABLE.name(),
|
||||
"Assignment target is not assignable",
|
||||
lValue.span());
|
||||
return new AssignmentTargetResolution(TypeView.unknown(), false);
|
||||
}
|
||||
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_UNRESOLVED.name(),
|
||||
"Assignment target '%s' does not resolve".formatted(rootName),
|
||||
lValue.span());
|
||||
@@ -701,7 +688,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
var currentReceiverIsThis = rootIsThis;
|
||||
if (rootIsThis) {
|
||||
if (receiverType == null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_INVALID_THIS_CONTEXT.name(),
|
||||
"Invalid 'this' usage outside struct/service methods and constructors",
|
||||
lValue.span());
|
||||
@@ -717,7 +704,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
} else if (model.constTypes.containsKey(rootName)) {
|
||||
currentType = model.constTypes.get(rootName);
|
||||
} else {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_ASSIGN_TARGET_UNRESOLVED.name(),
|
||||
"Assignment target '%s' does not resolve".formatted(rootName),
|
||||
lValue.span());
|
||||
@@ -728,8 +715,8 @@ final class PbsFlowBodyAnalyzer {
|
||||
for (int i = 0; i < lValue.pathSegments().size(); i++) {
|
||||
final var segment = lValue.pathSegments().get(i);
|
||||
final var isLastSegment = i == lValue.pathSegments().size() - 1;
|
||||
if (currentType.kind() != PbsFlowSemanticSupport.Kind.STRUCT) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
if (currentType.kind() != Kind.STRUCT) {
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_INVALID_MEMBER_ACCESS.name(),
|
||||
"Assignment target segment '%s' is not a struct field".formatted(segment),
|
||||
lValue.span());
|
||||
@@ -742,7 +729,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
final var field = struct.fields().get(segment);
|
||||
if (field == null) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_INVALID_MEMBER_ACCESS.name(),
|
||||
"Struct member '%s' does not exist".formatted(segment),
|
||||
lValue.span());
|
||||
@@ -750,11 +737,11 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
|
||||
final var ownerContext = receiverType != null
|
||||
&& receiverType.kind() == PbsFlowSemanticSupport.Kind.STRUCT
|
||||
&& receiverType.kind() == Kind.STRUCT
|
||||
&& receiverType.name().equals(currentType.name());
|
||||
if (isLastSegment) {
|
||||
if (!canWriteStructField(field, ownerContext, currentReceiverIsThis)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_FIELD_WRITE_ACCESS_DENIED.name(),
|
||||
"Write access to struct field '%s' is not permitted".formatted(segment),
|
||||
lValue.span());
|
||||
@@ -764,7 +751,7 @@ final class PbsFlowBodyAnalyzer {
|
||||
}
|
||||
|
||||
if (!canReadStructField(field, ownerContext, currentReceiverIsThis)) {
|
||||
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
|
||||
Diagnostics.error(diagnostics,
|
||||
PbsSemanticsErrors.E_SEM_FIELD_READ_ACCESS_DENIED.name(),
|
||||
"Read access to struct field '%s' is not permitted".formatted(segment),
|
||||
lValue.span());
|
||||
@@ -792,11 +779,8 @@ final class PbsFlowBodyAnalyzer {
|
||||
case MUL_ASSIGN -> "*";
|
||||
case DIV_ASSIGN -> "/";
|
||||
case MOD_ASSIGN -> "%";
|
||||
case ASSIGN -> "=";
|
||||
default -> throw new IllegalStateException("Unexpected value: " + operator);
|
||||
};
|
||||
if ("=".equals(binaryOperator)) {
|
||||
return typeOps.compatible(valueType, targetType);
|
||||
}
|
||||
final var resultType = typeOps.inferBinaryResult(binaryOperator, targetType, valueType);
|
||||
return typeOps.compatible(resultType, targetType);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user