-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscope.ts
221 lines (199 loc) · 5.63 KB
/
scope.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// This file is compiled and inlined in /glsl-grammar.pegjs. See build-parser.sh
// and note that file is called in parse.test.ts
import {
AstNode,
LocationObject,
ArraySpecifierNode,
FunctionPrototypeNode,
KeywordNode,
FunctionNode,
FunctionCallNode,
TypeNameNode,
} from '../ast/index.js';
import { xor } from './utils.js';
export type TypeScopeEntry = {
declaration?: TypeNameNode;
references: TypeNameNode[];
};
export type TypeScopeIndex = {
[name: string]: TypeScopeEntry;
};
export type ScopeEntry = { declaration?: AstNode; references: AstNode[] };
export type ScopeIndex = {
[name: string]: ScopeEntry;
};
export type FunctionOverloadDefinition = {
returnType: string;
parameterTypes: string[];
declaration?: FunctionNode;
references: (FunctionNode | FunctionCallNode | FunctionPrototypeNode)[];
};
export type FunctionOverloadIndex = {
[signature: string]: FunctionOverloadDefinition;
};
export type FunctionScopeIndex = {
[name: string]: FunctionOverloadIndex;
};
export type Scope = {
name: string;
parent?: Scope;
bindings: ScopeIndex;
types: TypeScopeIndex;
functions: FunctionScopeIndex;
location?: LocationObject;
};
export const UNKNOWN_TYPE = 'UNKNOWN TYPE';
export type FunctionSignature = [
returnType: string,
parameterTypes: string[],
signature: string
];
export const makeScopeIndex = (
firstReference: AstNode,
declaration?: AstNode
): ScopeEntry => ({
declaration,
references: [firstReference],
});
export const findTypeScope = (
scope: Scope | undefined,
typeName: string
): Scope | null => {
if (!scope) {
return null;
}
if (typeName in scope.types) {
return scope;
}
return findTypeScope(scope.parent, typeName);
};
export const isDeclaredType = (scope: Scope, typeName: string) =>
findTypeScope(scope, typeName) !== null;
export const findBindingScope = (
scope: Scope | undefined,
name: string
): Scope | null => {
if (!scope) {
return null;
}
if (name in scope.bindings) {
return scope;
}
return findBindingScope(scope.parent, name);
};
export const extractConstant = (expression: AstNode): string => {
let result = UNKNOWN_TYPE;
// Keyword case, like float
if ('token' in expression) {
result = expression.token;
// User defined type
} else if (
'identifier' in expression &&
typeof expression.identifier === 'string'
) {
result = expression.identifier;
} else {
console.warn(result, expression);
}
return result;
};
export const quantifiersSignature = (quantifier: ArraySpecifierNode[]) =>
quantifier.map((q) => `[${extractConstant(q.expression)}]`).join('');
export const functionDeclarationSignature = (
node: FunctionNode | FunctionPrototypeNode
): FunctionSignature => {
const proto = node.type === 'function' ? node.prototype : node;
const { specifier } = proto.header.returnType;
const quantifiers = specifier.quantifier || [];
const parameterTypes = proto?.parameters?.map(({ specifier, quantifier }) => {
const quantifiers =
// vec4[1][2] param
specifier.quantifier ||
// vec4 param[1][3]
quantifier ||
[];
return `${extractConstant(specifier.specifier)}${quantifiersSignature(
quantifiers
)}`;
}) || ['void'];
const returnType = `${
(specifier.specifier as KeywordNode).token
}${quantifiersSignature(quantifiers)}`;
return [
returnType,
parameterTypes,
`${returnType}: ${parameterTypes.join(', ')}`,
];
};
export const doSignaturesMatch = (
definitionSignature: string,
definition: FunctionOverloadDefinition,
callSignature: FunctionSignature
) => {
if (definitionSignature === callSignature[0]) {
return true;
}
const left = [definition.returnType, ...definition.parameterTypes];
const right = [callSignature[0], ...callSignature[1]];
// Special case. When comparing "a()" to "a(1)", a() has paramater VOID, and
// a(1) has type UNKNOWN. This will pass as true in the final check of this
// function, even though it's not.
if (left.length === 2 && xor(left[1] === 'void', right[1] === 'void')) {
return false;
}
return (
left.length === right.length &&
left.every(
(type, index) =>
type === right[index] ||
type === UNKNOWN_TYPE ||
right[index] === UNKNOWN_TYPE
)
);
};
export const findOverloadDefinition = (
signature: FunctionSignature,
index: FunctionOverloadIndex
): FunctionOverloadDefinition | undefined => {
return Object.entries(index).reduce<
ReturnType<typeof findOverloadDefinition>
>((found, [overloadSignature, overloadDefinition]) => {
return (
found ||
(doSignaturesMatch(overloadSignature, overloadDefinition, signature)
? overloadDefinition
: undefined)
);
}, undefined);
};
export const functionUseSignature = (
node: FunctionCallNode
): FunctionSignature => {
const parameterTypes =
node.args.length === 0
? ['void']
: node.args
.filter((arg) => (arg as any).literal !== ',')
.map(() => UNKNOWN_TYPE);
const returnType = UNKNOWN_TYPE;
return [
returnType,
parameterTypes,
`${returnType}: ${parameterTypes.join(', ')}`,
];
};
export const newOverloadIndex = (
returnType: string,
parameterTypes: string[],
firstReference: FunctionNode | FunctionCallNode | FunctionPrototypeNode,
declaration?: FunctionNode
): FunctionOverloadDefinition => ({
returnType,
parameterTypes,
declaration,
references: [firstReference],
});
export const findGlobalScope = (scope: Scope): Scope =>
scope.parent ? findGlobalScope(scope.parent) : scope;
export const isDeclaredFunction = (scope: Scope, fnName: string) =>
fnName in findGlobalScope(scope).functions;