Skip to content

Commit 991ad54

Browse files
committed
feat: scoping of arguments
1 parent b2cc2e4 commit 991ad54

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

src/language/scoping/safe-ds-scope-provider.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import {
1010
Scope,
1111
} from 'langium';
1212
import {
13+
isSdsAbstractCall,
14+
isSdsAnnotationCall,
15+
isSdsArgument,
1316
isSdsAssignment,
1417
isSdsBlock,
18+
isSdsCall,
1519
isSdsCallable,
1620
isSdsClass,
1721
isSdsEnum,
@@ -31,6 +35,7 @@ import {
3135
isSdsTypeArgument,
3236
isSdsWildcardImport,
3337
isSdsYield,
38+
SdsArgument,
3439
SdsDeclaration,
3540
SdsExpression,
3641
SdsImportedDeclaration,
@@ -61,6 +66,7 @@ import { isStatic } from '../helpers/checks.js';
6166
import { SafeDsServices } from '../safe-ds-module.js';
6267
import { SafeDsTypeComputer } from '../typing/safe-ds-type-computer.js';
6368
import { SafeDsPackageManager } from '../workspace/safe-ds-package-manager.js';
69+
import { CallableType, StaticType } from '../typing/model.js';
6470

6571
export class SafeDsScopeProvider extends DefaultScopeProvider {
6672
private readonly astReflection: AstReflection;
@@ -78,7 +84,9 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
7884
override getScope(context: ReferenceInfo): Scope {
7985
const node = context.container;
8086

81-
if (isSdsImportedDeclaration(node) && context.property === 'declaration') {
87+
if (isSdsArgument(node) && context.property === 'parameter') {
88+
return this.getScopeForArgumentParameter(node);
89+
} else if (isSdsImportedDeclaration(node) && context.property === 'declaration') {
8290
return this.getScopeForImportedDeclarationDeclaration(node);
8391
} else if (isSdsNamedType(node) && context.property === 'declaration') {
8492
if (isSdsMemberType(node.$container) && node.$containerProperty === 'member') {
@@ -101,6 +109,35 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
101109
}
102110
}
103111

112+
private getScopeForArgumentParameter(node: SdsArgument): Scope {
113+
const containingAbstractCall = getContainerOfType(node, isSdsAbstractCall);
114+
if (isSdsAnnotationCall(containingAbstractCall)) {
115+
const annotation = containingAbstractCall.annotation?.ref;
116+
if (!annotation) {
117+
return EMPTY_SCOPE;
118+
}
119+
120+
const parameters = parametersOrEmpty(annotation.parameterList);
121+
return this.createScopeForNodes(parameters);
122+
} else if (isSdsCall(containingAbstractCall)) {
123+
const receiverType = this.typeComputer.computeType(containingAbstractCall.receiver);
124+
if (receiverType instanceof CallableType) {
125+
const parameters = parametersOrEmpty(receiverType.sdsCallable.parameterList);
126+
return this.createScopeForNodes(parameters);
127+
} else if (receiverType instanceof StaticType) {
128+
const declaration = receiverType.instanceType.sdsDeclaration;
129+
if (isSdsCallable(declaration)) {
130+
const parameters = parametersOrEmpty(declaration.parameterList);
131+
return this.createScopeForNodes(parameters);
132+
}
133+
}
134+
135+
return EMPTY_SCOPE;
136+
} /* c8 ignore start */ else {
137+
return EMPTY_SCOPE;
138+
} /* c8 ignore stop */
139+
}
140+
104141
private getScopeForImportedDeclarationDeclaration(node: SdsImportedDeclaration): Scope {
105142
const ownPackageName = packageNameOrNull(node);
106143

src/language/typing/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class CallableType extends Type {
2323
override isNullable: boolean = false;
2424

2525
constructor(
26-
readonly callable: SdsCallable,
26+
readonly sdsCallable: SdsCallable,
2727
readonly inputType: NamedTupleType,
2828
readonly outputType: NamedTupleType,
2929
) {
@@ -48,7 +48,7 @@ export class CallableType extends Type {
4848
}
4949

5050
return (
51-
other.callable === this.callable &&
51+
other.sdsCallable === this.sdsCallable &&
5252
other.inputType.equals(this.inputType) &&
5353
other.outputType.equals(this.outputType)
5454
);

src/language/typing/safe-ds-type-computer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,12 @@ export class SafeDsTypeComputer {
357357
const receiverType = this.computeType(node.receiver);
358358

359359
if (receiverType instanceof CallableType) {
360-
if (!isSdsAnnotation(receiverType.callable)) {
360+
if (!isSdsAnnotation(receiverType.sdsCallable)) {
361361
return receiverType.outputType;
362362
}
363363
} else if (receiverType instanceof StaticType) {
364364
const instanceType = receiverType.instanceType;
365-
const declaration = instanceType.sdsDeclaration;
366-
367-
if (isSdsClass(declaration) || isSdsEnumVariant(declaration)) {
365+
if (isSdsCallable(instanceType.sdsDeclaration)) {
368366
return instanceType;
369367
}
370368
}

tests/resources/scoping/arguments/of annotation calls/main.sdstest renamed to tests/resources/scoping/arguments/of annotation calls/to parameter/main.sdstest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tests.scoping.arguments.ofAnnotationCalls
1+
package tests.scoping.arguments.ofAnnotationCalls.toParameter
22

33
annotation MyAnnotation(
44
// $TEST$ target a
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tests.scoping.arguments.ofAnnotationCalls.toSomethingOtherThanParameter
2+
3+
annotation MyAnnotation(a: Int)
4+
5+
@MyAnnotation(
6+
// $TEST$ unresolved
7+
»MyAnnotation« = 0,
8+
)
9+
pipeline myPipeline {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tests.scoping.arguments.ofAnnotationCalls.unresolved
2+
3+
annotation MyAnnotation(a: Int)
4+
5+
@MyAnnotation(
6+
// $TEST$ unresolved
7+
»unresolved« = 0,
8+
)
9+
@Unresolved(
10+
// $TEST$ unresolved
11+
»a« = 0
12+
)
13+
pipeline myPipeline {}

0 commit comments

Comments
 (0)