Skip to content

Improve handling of generic specialization and constraints #367

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 6 commits into from
Apr 30, 2025
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
24 changes: 20 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- **API:** `EnumeratorOccurrence` type.
- **API:** `EnumeratorOccurrence::getGetEnumerator` method.
- **API:** `EnumeratorOccurrence::getMoveNext` method.
- **API:** `EnumeratorOccurrence::getCurrent` method.
- **API:** `ForInStatementNode.getEnumeratorOccurrence` method.
- **API:** `TypeOfTypeNode::getTypeReferenceNode` method.
- **API:** `NameReferenceNode::getFirstName` method.
- **API:** `DelphiTokenType.TYPE_CONSTRAINT` token type.
- **API:** `ConstraintNode` node type.
- **API:** `ClassConstraintNode` node type.
- **API:** `ConstructorConstraintNode` node type.
- **API:** `RecordConstraintNode` node type.
- **API:** `TypeConstraintNode` node type.
- **API:** `TypeParameterNode::getConstraintNodes` method.
- **API:** `TypeParameterType::constraintItems` method.
- **API:** `Constraint` type.
- **API:** `Constraint.ClassConstraint` type.
- **API:** `Constraint.ConstructorConstraint` type.
- **API:** `Constraint.RecordConstraint` type.
- **API:** `Constraint.TypeConstraint` type.

### Changed

- Detect tab-indented multiline strings in `TabulationCharacter`.
- Improve support for evaluating name references in compiler directive expressions.
- Improve overload resolution in cases involving generic type parameter constraints.

### Deprecated

Expand All @@ -29,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`getEnumeratorOccurrence` instead.
- **API:** `ForInStatementNode::getCurrentDeclaration` method, get the declaration through
`getEnumeratorOccurrence` instead.
- **API:** `TypeParameterNode::getTypeConstraintNodes` method, use `getConstraintNodes` instead.
- **API:** `TypeParameterType::constraints` method, use `constraintItems` instead.

### Fixed

Expand All @@ -37,7 +51,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- False positives on enumerator property `Current` in `UnusedProperty`.
- False positives on enums that are never referenced by name (but have used values) in `UnusedType`.
- Name resolution failures in legacy initialization sections referencing the implementation section.
- Name resolution failures whena accessing ancestors of enclosing types from nested type methods.
- Name resolution failures when accessing ancestors of enclosing types from nested type methods.
- Name resolution failures on invocations of methods with generic open array parameters.
- Name resolution failures around `Create` calls on types with `constructor` constraints.
- Incorrect file position calculation for multiline string tokens.
- Analysis errors around `type of` type declarations.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ tokens {
TkCompilerDirective;
TkRealNumber;
TkTypeParameter;
TkTypeConstraint;
TkForLoopVar;
TkArrayAccessorNode;
TkArrayConstructor;
Expand Down Expand Up @@ -780,10 +781,10 @@ typeParameter : nameDeclaration (',' nameDeclaration)* (':' gene
nameDeclaration (nameDeclaration)* (genericConstraint genericConstraint*)?
)
;
genericConstraint : typeReference
| RECORD
| CLASS
| CONSTRUCTOR
genericConstraint : typeReference -> ^(TkTypeConstraint<TypeConstraintNodeImpl> typeReference)
| RECORD<RecordConstraintNodeImpl>^
| CLASS<ClassConstraintNodeImpl>^
| CONSTRUCTOR<ConstructorConstraintNodeImpl>^
;
genericArguments : '<' typeReferenceOrStringOrFile (',' typeReferenceOrStringOrFile)* '>'
-> ^(TkGenericArguments<GenericArgumentsNodeImpl> '<' typeReferenceOrStringOrFile (',' typeReferenceOrStringOrFile)* '>')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Sonar Delphi Plugin
* Copyright (C) 2025 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.antlr.ast.node;

import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import au.com.integradev.delphi.type.generic.constraint.ClassConstraintImpl;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.ClassConstraintNode;
import org.sonar.plugins.communitydelphi.api.type.Constraint;

public final class ClassConstraintNodeImpl extends DelphiNodeImpl implements ClassConstraintNode {
public ClassConstraintNodeImpl(Token token) {
super(token);
}

@Override
public <T> T accept(DelphiParserVisitor<T> visitor, T data) {
return visitor.visit(this, data);
}

@Override
public Constraint getConstraint() {
return ClassConstraintImpl.instance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Sonar Delphi Plugin
* Copyright (C) 2025 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.antlr.ast.node;

import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import au.com.integradev.delphi.type.generic.constraint.ConstructorConstraintImpl;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.ConstructorConstraintNode;
import org.sonar.plugins.communitydelphi.api.type.Constraint;

public final class ConstructorConstraintNodeImpl extends DelphiNodeImpl
implements ConstructorConstraintNode {
public ConstructorConstraintNodeImpl(Token token) {
super(token);
}

@Override
public <T> T accept(DelphiParserVisitor<T> visitor, T data) {
return visitor.visit(this, data);
}

@Override
public Constraint getConstraint() {
return ConstructorConstraintImpl.instance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.util.List;
import java.util.stream.Collectors;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.ConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
import org.sonar.plugins.communitydelphi.api.ast.GenericDefinitionNode;
import org.sonar.plugins.communitydelphi.api.ast.NameDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeParameterNode;
import org.sonar.plugins.communitydelphi.api.type.Type;
import org.sonar.plugins.communitydelphi.api.type.Constraint;
import org.sonar.plugins.communitydelphi.api.type.Type.TypeParameterType;
import org.sonar.plugins.communitydelphi.api.type.Typed;

public final class GenericDefinitionNodeImpl extends DelphiNodeImpl
implements GenericDefinitionNode {
Expand All @@ -56,9 +56,9 @@ public List<TypeParameter> getTypeParameters() {
ImmutableList.Builder<TypeParameter> builder = ImmutableList.builder();

for (TypeParameterNode parameterNode : getTypeParameterNodes()) {
List<Type> constraints =
parameterNode.getTypeConstraintNodes().stream()
.map(Typed::getType)
List<Constraint> constraints =
parameterNode.getConstraintNodes().stream()
.map(ConstraintNode::getConstraint)
.collect(Collectors.toUnmodifiableList());

for (NameDeclarationNode name : parameterNode.getTypeParameterNameNodes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ public Type getType() {
}
}

@Override
public NameReferenceNode getFirstName() {
NameReferenceNode result = this;
while (result.getParent() instanceof NameReferenceNode) {
result = (NameReferenceNode) result.getParent();
}
return result;
}

@Override
public NameReferenceNode getLastName() {
List<NameReferenceNode> flatNames = flatten();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Sonar Delphi Plugin
* Copyright (C) 2025 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.antlr.ast.node;

import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import au.com.integradev.delphi.type.generic.constraint.RecordConstraintImpl;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.RecordConstraintNode;
import org.sonar.plugins.communitydelphi.api.type.Constraint;

public final class RecordConstraintNodeImpl extends DelphiNodeImpl implements RecordConstraintNode {
public RecordConstraintNodeImpl(Token token) {
super(token);
}

@Override
public <T> T accept(DelphiParserVisitor<T> visitor, T data) {
return visitor.visit(this, data);
}

@Override
public Constraint getConstraint() {
return RecordConstraintImpl.instance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Sonar Delphi Plugin
* Copyright (C) 2025 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.antlr.ast.node;

import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import au.com.integradev.delphi.type.generic.constraint.TypeConstraintImpl;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.TypeConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeReferenceNode;
import org.sonar.plugins.communitydelphi.api.type.Constraint;

public final class TypeConstraintNodeImpl extends DelphiNodeImpl implements TypeConstraintNode {
private Constraint constraint;

public TypeConstraintNodeImpl(Token token) {
super(token);
}

public TypeConstraintNodeImpl(int tokenType) {
super(tokenType);
}

@Override
public <T> T accept(DelphiParserVisitor<T> visitor, T data) {
return visitor.visit(this, data);
}

@Override
public TypeReferenceNode getTypeNode() {
return (TypeReferenceNode) getChild(0);
}

@Override
public Constraint getConstraint() {
if (constraint == null) {
constraint = new TypeConstraintImpl(getTypeNode().getType());
}
return constraint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

import au.com.integradev.delphi.antlr.ast.visitors.DelphiParserVisitor;
import java.util.List;
import java.util.stream.Collectors;
import org.antlr.runtime.Token;
import org.sonar.plugins.communitydelphi.api.ast.ConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.NameDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeParameterNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeReferenceNode;

Expand All @@ -44,8 +47,18 @@ public List<NameDeclarationNode> getTypeParameterNameNodes() {
return findChildrenOfType(NameDeclarationNode.class);
}

@SuppressWarnings("removal")
@Override
public List<TypeReferenceNode> getTypeConstraintNodes() {
return findChildrenOfType(TypeReferenceNode.class);
return getConstraintNodes().stream()
.filter(TypeConstraintNode.class::isInstance)
.map(TypeConstraintNode.class::cast)
.map(TypeConstraintNode::getTypeNode)
.collect(Collectors.toList());
}

@Override
public List<ConstraintNode> getConstraintNodes() {
return findChildrenOfType(ConstraintNode.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.sonar.plugins.communitydelphi.api.ast.BinaryExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.CaseItemStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.CaseStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.ClassConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.ClassHelperTypeNode;
import org.sonar.plugins.communitydelphi.api.ast.ClassReferenceTypeNode;
import org.sonar.plugins.communitydelphi.api.ast.ClassTypeNode;
Expand All @@ -49,6 +50,8 @@
import org.sonar.plugins.communitydelphi.api.ast.ConstDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.ConstSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.ConstStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.ConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.ConstructorConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.ContainsClauseNode;
import org.sonar.plugins.communitydelphi.api.ast.DelphiAst;
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
Expand Down Expand Up @@ -117,6 +120,7 @@
import org.sonar.plugins.communitydelphi.api.ast.RaiseStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.RangeExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.RealLiteralNode;
import org.sonar.plugins.communitydelphi.api.ast.RecordConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.RecordExpressionItemNode;
import org.sonar.plugins.communitydelphi.api.ast.RecordExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.RecordHelperTypeNode;
Expand Down Expand Up @@ -144,6 +148,7 @@
import org.sonar.plugins.communitydelphi.api.ast.SubRangeTypeNode;
import org.sonar.plugins.communitydelphi.api.ast.TextLiteralNode;
import org.sonar.plugins.communitydelphi.api.ast.TryStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeConstraintNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeOfTypeNode;
Expand Down Expand Up @@ -745,4 +750,25 @@ default T visit(WhileStatementNode node, T data) {
default T visit(WithStatementNode node, T data) {
return visit((StatementNode) node, data);
}

/* Constraints */
default T visit(ConstraintNode node, T data) {
return visit((DelphiNode) node, data);
}

default T visit(ClassConstraintNode node, T data) {
return visit((ConstraintNode) node, data);
}

default T visit(ConstructorConstraintNode node, T data) {
return visit((ConstraintNode) node, data);
}

default T visit(RecordConstraintNode node, T data) {
return visit((ConstraintNode) node, data);
}

default T visit(TypeConstraintNode node, T data) {
return visit((ConstraintNode) node, data);
}
}
Loading
Loading