-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunction.ts
More file actions
381 lines (311 loc) · 10.9 KB
/
Copy pathfunction.ts
File metadata and controls
381 lines (311 loc) · 10.9 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { Declaration } from "./declaration.js";
import { TemplateDeclaration } from "./templateDeclaration.js";
import { Namespace, Flags } from "./namespace.js";
import { State, Dependency, Dependencies, ReasonKind, ResolverContext } from "../target.js";
import { Writer } from "../writer.js";
import { Type } from "../type/type.js";
import { TemplateType } from "../type/templateType.js";
import { TypeQualifier } from "../type/qualifiedType.js";
import { FunctionType } from "../type/functionType.js";
import { FUNCTION_TYPE, UNION_TYPE, VOID_TYPE } from "../type/namedType.js";
export class Parameter {
private type: Type;
private readonly name: string;
private readonly defaultValue?: string;
public constructor(type: Type, name: string, defaultValue?: string) {
this.type = type;
this.name = name;
this.defaultValue = defaultValue;
}
public getType(): Type {
return this.type;
}
public setType(type: Type): void {
this.type = type;
}
public getName(): string {
return this.name;
}
public getDefaultValue(): string | undefined {
return this.defaultValue;
}
}
export class Initializer {
private readonly name: string;
private readonly value: string;
public constructor(name: string, value: string) {
this.name = name;
this.value = value;
}
public getName(): string {
return this.name;
}
public getValue(): string {
return this.value;
}
}
export class Function extends TemplateDeclaration {
// Function parameters.
private parameters?: Array<Parameter>;
// Constructor initializers, mostly used for extensions in
// "src/extensions.ts".
private initializers?: Array<Initializer>;
// We can't track dependencies of the function body, they must be added
// manually using `addExtraDependency`.
private extraDependencies?: Dependencies;
// The return type.
private type?: Type;
// The function body. This is mostly used for extensions in
// "src/extensions.ts", but also for some automatically generated
// forwarding helper functions.
private body?: string;
public constructor(name: string, type?: Type, namespace?: Namespace) {
super(name, namespace);
this.type = type;
}
public isConstructor(): boolean {
return this.getName() === this.getParent()?.getName();
}
public getParameters(): ReadonlyArray<Parameter> {
return this.parameters ?? [];
}
public addParameter(type: Type, name: string, defaultValue?: string): void {
this.parameters ??= [];
this.parameters.push(new Parameter(type, name, defaultValue));
}
public getInitializers(): ReadonlyArray<Initializer> {
return this.initializers ?? [];
}
public addInitializer(name: string, value: string): void {
this.initializers ??= [];
this.initializers.push(new Initializer(name, value));
}
public getExtraDependencies(): Dependencies {
return this.extraDependencies ?? new Dependencies;
}
public addExtraDependency(declaration: Declaration, state: State, reason: ReasonKind = ReasonKind.Extra): void {
this.extraDependencies ??= new Dependencies;
this.extraDependencies.add(declaration, new Dependency(state, this, reason));
}
public getType(): Type | undefined {
return this.type;
}
public setType(type: Type | undefined): void {
this.type = type;
}
public getBody(): string | undefined {
return this.body;
}
public setBody(body: string): void {
this.body = body;
}
public maxState(): State {
return State.Partial;
}
public getChildren(): ReadonlyArray<Declaration> {
return new Array;
}
// The dependencies of a function are:
// - partial for types used in function parameters.
// - partial for the return type.
// - extra dependencies added using `addExtraDependency`.
protected getDirectDependencies(state: State): Dependencies {
const parameterReason = new Dependency(State.Partial, this, ReasonKind.ParameterType);
const returnReason = new Dependency(State.Partial, this, ReasonKind.ReturnType);
return new Dependencies(
this.getParameters()
.flatMap(parameter => [...parameter.getType().getDependencies(parameterReason)])
.concat([...this.type?.getDependencies(returnReason) ?? []])
.concat([...this.extraDependencies ?? []])
);
}
protected getDirectReferencedTypes(): ReadonlyArray<Type> {
return this.getParameters()
.flatMap(parameter => [...parameter.getType().getReferencedTypes()])
.concat([...this.type?.getReferencedTypes() ?? []]);
}
protected writeImpl(context: ResolverContext, writer: Writer, state: State, namespace?: Namespace): void {
// 1. Write the template<...> line, if needed.
this.writeTemplate(writer, state, namespace);
// 2. Write the interface name attribute, unless there is a body.
if (this.body === undefined) {
this.writeInterfaceName(writer);
}
// 3. Write attributes.
if (this.getAttributes().length > 0) {
this.writeAttributes(writer);
writer.writeLine(false);
}
// 4. Write leading modifiers.
const flags = this.getFlags();
if (flags & Flags.Explicit) {
writer.write("explicit");
writer.writeSpace();
}
if (flags & Flags.Static) {
writer.write("static");
writer.writeSpace();
}
if (flags & Flags.Inline) {
writer.write("inline");
writer.writeSpace();
}
// 5. Write return type.
if (this.type) {
this.type.write(writer, namespace);
writer.writeSpace();
}
// 6. Write function name.
writer.write(this.getName());
writer.write("(");
let first = true;
// 7. Write function parameters
for (const parameter of this.getParameters()) {
const defaultValue = parameter.getDefaultValue();
if (!first) {
writer.write(",");
writer.writeSpace(false);
}
parameter.getType().write(writer, namespace);
writer.writeSpace();
writer.write(parameter.getName());
if (defaultValue) {
writer.writeSpace(false);
writer.write("=");
writer.writeSpace(false);
writer.write(defaultValue);
}
first = false;
}
writer.write(")");
first = true;
// 8. Write trailing modifiers.
if (flags & Flags.Const) {
writer.writeSpace(!first);
writer.write("const");
first = false;
}
if (flags & Flags.Noexcept) {
writer.writeSpace(!first);
writer.write("noexcept");
first = false;
}
first = true;
// 9. Write constructor initializers.
for (const initializer of this.getInitializers()) {
writer.write(first ? ":" : ",");
writer.writeSpace(false);
writer.write(initializer.getName());
writer.write("(");
writer.write(initializer.getValue());
writer.write(")");
first = false;
}
// 10. Write body, if present.
if (this.body !== undefined) {
writer.writeBody(this.body);
} else {
writer.write(";");
writer.writeLine(false);
}
}
// Merge function types to remove duplicate declarations and avoid
// ambiguous overloads. Two functions are merged if and only if they have:
// - An equal number of parameters.
// - An equal number of template type parameters.
// - Equal constness.
// - Compatible parameter types.
//
// For each parameter type of this function and the corresponding type of
// the other function:
// - If both are the same type, it is used unchanged.
// - If both are `_Function` types, they are merged using `mergeFunction`.
// - If both are `_Union` types, they are merged by creating a new `_Union`
// with all type parameters from both `_Union`s.
// - Otherwise, the parameters types are incompatible.
public merge(other: Declaration): boolean {
// Cannot merge if the other declaration is not also a function.
if (!(other instanceof Function)) {
return false;
}
const thisParameters = this.getParameters();
const otherParameters = other.getParameters();
const parameters = new Array;
let canMerge = true;
canMerge &&= thisParameters.length === otherParameters.length;
canMerge &&= this.getTypeParameters().length === other.getTypeParameters().length;
canMerge &&= !((this.getFlags() ^ other.getFlags()) & Flags.Const);
if (!canMerge) {
return false;
}
for (let i = 0; i < thisParameters.length; i++) {
if (thisParameters[i].getType() === otherParameters[i].getType()) {
// Both parameters are the same type, use the type unchanged.
parameters.push(thisParameters[i].getType());
continue;
}
const thisParameter = thisParameters[i].getType().removeQualifiers();
const otherParameter = otherParameters[i].getType().removeQualifiers();
if (!(thisParameter instanceof TemplateType && otherParameter instanceof TemplateType)) {
return false;
}
const thisInner = thisParameter.getInner();
const otherInner = otherParameter.getInner();
if (thisInner === UNION_TYPE && otherInner === UNION_TYPE) {
// Both parameters are union types, create a new union with all
// type parameters from both unions.
parameters.push(TemplateType.createUnion(
TypeQualifier.ConstReference,
...thisParameter.getTypeParameters() as ReadonlyArray<Type>,
...otherParameter.getTypeParameters() as ReadonlyArray<Type>
));
} else if (thisInner === FUNCTION_TYPE && otherInner === FUNCTION_TYPE) {
// Both parameters are function types, use `mergeFunction` to
// merge the function types.
const thisFunction = thisParameter.getTypeParameters()[0] as FunctionType;
const otherFunction = otherParameter.getTypeParameters()[0] as FunctionType;
parameters.push(mergeFunction(thisFunction, otherFunction));
} else {
// Parameter types are not compatible.
return false;
}
}
// At this point we know that the function declarations can be merged
// for sure. We can now safely modify the declaration.
this.type = mergeReturn(this.type, other.type);
for (let i = 0; i < parameters.length; i++) {
thisParameters[i].setType(parameters[i]);
}
return true;
}
}
// Merge `_Function` types. The return type is made using `mergeReturn`. Every
// parameter is a `_Union` type of the parameter type from the first function
// and the corresponding parameter type of the second function. If one function
// has more parameters than the other, the excess parameters are added to the
// returned function unmodified.
function mergeFunction(self: FunctionType, other: FunctionType): Type {
const selfParameters = self.getParameters();
const otherParameters = other.getParameters();
if (otherParameters.length < selfParameters.length) {
return mergeFunction(other, self);
}
const parameters = selfParameters
.map((parameter, i) => TemplateType.createUnion(TypeQualifier.Pointer, parameter, otherParameters[i]))
.concat(otherParameters.slice(selfParameters.length));
return TemplateType.createFunction(
mergeReturn(self.getReturnType(), other.getReturnType())!,
...parameters
).constReference();
}
// Merge return types. If either type is undefined or void, return the other
// type. Otherwise, return a `_Union` template of both types.
function mergeReturn(self?: Type, other?: Type): Type | undefined {
if (self === undefined || self === VOID_TYPE) {
return other;
} else if (other === undefined || other === VOID_TYPE) {
return self;
} else {
return TemplateType.createUnion(TypeQualifier.Pointer, self, other);
}
}