Skip to content

Fix various intrinsic signatures around dynamic and open arrays #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Various intrinsic routines had incorrect signatures around dynamic and open arrays.

## [1.12.0] - 2024-12-02

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class TypeFactoryImpl implements TypeFactory {
private final EnumMap<IntrinsicType, Type> intrinsicTypes;
private final IntegerSubrangeType anonymousUInt15;
private final IntegerSubrangeType anonymousUInt31;
private final IntegerType openArraySizeType;
private final IntegerType dynamicArraySizeType;
private final PointerType nilPointer;
private final FileType untypedFile;
private final CollectionType emptySet;
Expand All @@ -110,6 +112,18 @@ public TypeFactoryImpl(Toolchain toolchain, CompilerVersion compilerVersion) {
":AnonymousUInt31",
BigInteger.ZERO,
((IntegerType) getIntrinsic(IntrinsicType.INTEGER)).max());

if (toolchain.architecture == Architecture.X86) {
this.openArraySizeType = (IntegerType) getIntrinsic(IntrinsicType.INTEGER);
this.dynamicArraySizeType = (IntegerType) getIntrinsic(IntrinsicType.INTEGER);
} else {
IntrinsicType openArraySizeIntrinsic =
compilerVersion.compareTo(VERSION_ATHENS) >= 0
? IntrinsicType.NATIVEINT
: IntrinsicType.INTEGER;
this.openArraySizeType = (IntegerType) getIntrinsic(openArraySizeIntrinsic);
this.dynamicArraySizeType = (IntegerType) getIntrinsic(IntrinsicType.NATIVEINT);
}
}

private boolean isReal48Bit() {
Expand Down Expand Up @@ -556,6 +570,14 @@ public IntegerSubrangeType anonymousUInt31() {
return anonymousUInt31;
}

public IntegerType openArraySizeType() {
return openArraySizeType;
}

public IntegerType dynamicArraySizeType() {
return dynamicArraySizeType;
}

@Override
public IntegerType integerFromLiteralValue(BigInteger value) {
return intrinsicTypes.values().stream()
Expand All @@ -565,12 +587,4 @@ public IntegerType integerFromLiteralValue(BigInteger value) {
.findFirst()
.orElseThrow(IllegalStateException::new);
}

public CompilerVersion getCompilerVersion() {
return compilerVersion;
}

public Toolchain getToolchain() {
return toolchain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
public final class IntrinsicArgumentMatcher extends TypeImpl {
public static final Type LIKE_DYNAMIC_ARRAY =
new IntrinsicArgumentMatcher(
"<dynamic array, array constructor, string, or char>",
type ->
type.isDynamicArray()
|| type.isArrayConstructor()
|| type.isString()
|| type.isChar());
"<dynamic array, array constructor>",
type -> type.isDynamicArray() || type.isArrayConstructor());

public static final Type ANY_STRING =
new IntrinsicArgumentMatcher("<string or char>", type -> type.isString() || type.isChar());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import static org.sonar.plugins.communitydelphi.api.type.IntrinsicType.UNICODESTRING;
import static org.sonar.plugins.communitydelphi.api.type.IntrinsicType.WIDECHAR;

import au.com.integradev.delphi.compiler.Architecture;
import au.com.integradev.delphi.compiler.CompilerVersion;
import au.com.integradev.delphi.type.TypeImpl;
import au.com.integradev.delphi.type.factory.ArrayOption;
import au.com.integradev.delphi.type.factory.TypeFactoryImpl;
Expand All @@ -42,8 +40,6 @@
import org.sonar.plugins.communitydelphi.api.type.TypeFactory;

public abstract class IntrinsicReturnType extends TypeImpl {
private static final CompilerVersion VERSION_ATHENS = CompilerVersion.fromVersionNumber("36.0");

@Override
public String getImage() {
return "<" + getClass().getSimpleName() + ">";
Expand Down Expand Up @@ -97,25 +93,6 @@ public static Type argumentByIndex(int index) {
return new ArgumentByIndexReturnType(index);
}

private static Type getOpenArraySizeType(TypeFactory typeFactory) {
if (((TypeFactoryImpl) typeFactory).getToolchain().architecture == Architecture.X86) {
return typeFactory.getIntrinsic(IntrinsicType.INTEGER);
} else {
return typeFactory.getIntrinsic(
((TypeFactoryImpl) typeFactory).getCompilerVersion().compareTo(VERSION_ATHENS) >= 0
? IntrinsicType.NATIVEINT
: IntrinsicType.INTEGER);
}
}

private static Type getDynamicArraySizeType(TypeFactory typeFactory) {
if (((TypeFactoryImpl) typeFactory).getToolchain().architecture == Architecture.X86) {
return typeFactory.getIntrinsic(IntrinsicType.INTEGER);
} else {
return typeFactory.getIntrinsic(IntrinsicType.NATIVEINT);
}
}

private static final class LengthReturnType extends IntrinsicReturnType {
private final Type byteType;
private final Type integerType;
Expand All @@ -126,8 +103,8 @@ private static final class LengthReturnType extends IntrinsicReturnType {
private LengthReturnType(TypeFactory typeFactory) {
this.byteType = typeFactory.getIntrinsic(IntrinsicType.BYTE);
this.integerType = typeFactory.getIntrinsic(IntrinsicType.INTEGER);
this.openArraySizeType = getOpenArraySizeType(typeFactory);
this.dynamicArraySizeType = getDynamicArraySizeType(typeFactory);
this.openArraySizeType = ((TypeFactoryImpl) typeFactory).openArraySizeType();
this.dynamicArraySizeType = ((TypeFactoryImpl) typeFactory).dynamicArraySizeType();
}

@Override
Expand All @@ -152,8 +129,8 @@ private static final class HighLowReturnType extends IntrinsicReturnType {

private HighLowReturnType(TypeFactory typeFactory) {
this.integerType = typeFactory.getIntrinsic(IntrinsicType.INTEGER);
this.openArraySizeType = getOpenArraySizeType(typeFactory);
this.dynamicArraySizeType = getDynamicArraySizeType(typeFactory);
this.openArraySizeType = ((TypeFactoryImpl) typeFactory).openArraySizeType();
this.dynamicArraySizeType = ((TypeFactoryImpl) typeFactory).dynamicArraySizeType();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import au.com.integradev.delphi.symbol.declaration.TypeNameDeclarationImpl;
import au.com.integradev.delphi.symbol.declaration.VariableNameDeclarationImpl;
import au.com.integradev.delphi.symbol.scope.DelphiScopeImpl;
import au.com.integradev.delphi.type.factory.TypeFactoryImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -87,6 +88,14 @@ private Type type(IntrinsicType type) {
return typeFactory.getIntrinsic(type);
}

private Type openArraySizeType() {
return ((TypeFactoryImpl) typeFactory).openArraySizeType();
}

private Type dynamicArraySizeType() {
return ((TypeFactoryImpl) typeFactory).dynamicArraySizeType();
}

private void buildRoutines() {
routine("Abs").param(type(REAL)).returns(type(REAL));
routine("Abs").param(type(INTEGER)).returns(type(INTEGER));
Expand Down Expand Up @@ -162,6 +171,11 @@ private void buildRoutines() {
.param(LIKE_DYNAMIC_ARRAY)
.variadic(LIKE_DYNAMIC_ARRAY)
.returns(IntrinsicReturnType.concat(typeFactory));
routine("Concat")
.param(ANY_STRING)
.param(ANY_STRING)
.variadic(ANY_STRING)
.returns(IntrinsicReturnType.concat(typeFactory));
routine("Continue");
routine("Copy")
.param(type(PANSICHAR))
Expand All @@ -174,17 +188,26 @@ private void buildRoutines() {
.param(type(INTEGER))
.returns(IntrinsicReturnType.copy(typeFactory));
routine("Copy")
.param(LIKE_DYNAMIC_ARRAY)
.param(ANY_STRING)
.param(type(INTEGER))
.param(type(INTEGER))
.returns(IntrinsicReturnType.copy(typeFactory));
routine("Copy")
.param(LIKE_DYNAMIC_ARRAY)
.param(dynamicArraySizeType())
.param(dynamicArraySizeType())
.required(1)
.returns(IntrinsicReturnType.copy(typeFactory));
routine("Dec").varParam(ANY_ORDINAL).param(type(INTEGER)).required(1);
routine("Dec").varParam(ANY_TYPED_POINTER).param(type(INTEGER)).required(1);
routine("Default")
.param(ANY_CLASS_REFERENCE)
.returns(IntrinsicReturnType.classReferenceValue());
routine("Delete").varParam(LIKE_DYNAMIC_ARRAY).param(type(INTEGER)).param(type(INTEGER));
routine("Delete").varParam(ANY_STRING).param(type(INTEGER)).param(type(INTEGER));
routine("Delete")
.varParam(LIKE_DYNAMIC_ARRAY)
.param(dynamicArraySizeType())
.param(dynamicArraySizeType());
routine("Dispose").varParam(ANY_POINTER);
routine("Eof").varParam(ANY_FILE).required(0).returns(type(BOOLEAN));
routine("Eoln").varParam(ANY_FILE).required(0).returns(type(BOOLEAN));
Expand All @@ -209,7 +232,11 @@ private void buildRoutines() {
routine("Inc").varParam(ANY_TYPED_POINTER).param(type(INTEGER)).required(1);
routine("Include").varParam(ANY_SET).param(ANY_ORDINAL);
routine("Initialize").varParam(TypeFactory.untypedType()).param(type(NATIVEINT)).required(1);
routine("Insert").param(LIKE_DYNAMIC_ARRAY).varParam(LIKE_DYNAMIC_ARRAY).param(type(INTEGER));
routine("Insert").param(ANY_STRING).varParam(ANY_STRING).param(type(INTEGER));
routine("Insert")
.param(LIKE_DYNAMIC_ARRAY)
.varParam(LIKE_DYNAMIC_ARRAY)
.param(dynamicArraySizeType());
routine("IsConstValue").param(TypeFactory.untypedType()).returns(type(BOOLEAN));
routine("IsManagedType").param(ANY_CLASS_REFERENCE).returns(type(BOOLEAN));
routine("Length").param(type(SHORTSTRING)).returns(IntrinsicReturnType.length(typeFactory));
Expand Down Expand Up @@ -251,7 +278,11 @@ private void buildRoutines() {
routine("Seek").varParam(ANY_FILE).param(type(INTEGER));
routine("SeekEof").varParam(ANY_TEXT_FILE).required(0).returns(type(BOOLEAN));
routine("SeekEoln").varParam(ANY_TEXT_FILE).required(0).returns(type(BOOLEAN));
routine("SetLength").varParam(LIKE_DYNAMIC_ARRAY).param(type(INTEGER)).variadic(type(INTEGER));
routine("SetLength").varParam(ANY_STRING).param(type(INTEGER));
routine("SetLength")
.varParam(LIKE_DYNAMIC_ARRAY)
.param(dynamicArraySizeType())
.variadic(dynamicArraySizeType());
routine("SetString").varParam(ANY_STRING).param(type(PANSICHAR)).param(type(INTEGER));
routine("SetString").varParam(ANY_STRING).param(type(PWIDECHAR)).param(type(INTEGER));
routine("SetTextBuf")
Expand All @@ -262,7 +293,7 @@ private void buildRoutines() {
routine("SizeOf").param(TypeFactory.untypedType()).returns(type(INTEGER));
routine("Slice")
.varParam(ANY_ARRAY)
.param(type(INTEGER))
.param(openArraySizeType())
.returns(IntrinsicReturnType.slice(typeFactory));
routine("Sqr").param(type(EXTENDED)).returns(type(EXTENDED));
routine("Sqr").param(type(INTEGER)).returns(type(INTEGER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void testLikeDynamicArray() {

assertThat(matches(LIKE_DYNAMIC_ARRAY, dynamicArray)).isTrue();
assertThat(matches(LIKE_DYNAMIC_ARRAY, arrayConstructor)).isTrue();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.ANSICHAR))).isTrue();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.WIDECHAR))).isTrue();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.ANSICHAR))).isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.WIDECHAR))).isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.SHORTSTRING)))
.isTrue();
.isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.ANSISTRING)))
.isTrue();
.isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.getIntrinsic(IntrinsicType.UNICODESTRING)))
.isTrue();
.isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, fixedArray)).isFalse();
assertThat(matches(LIKE_DYNAMIC_ARRAY, FACTORY.emptySet())).isFalse();
}
Expand Down
Loading