forked from jsx/JSX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.js
More file actions
609 lines (465 loc) · 14.6 KB
/
Copy pathtype.js
File metadata and controls
609 lines (465 loc) · 14.6 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* Copyright (c) 2012 DeNA Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
var Class = require("./Class");
eval(Class.$import("./classdef"));
eval(Class.$import("./util"));
"use strict";
var Type = exports.Type = Class.extend({
$_initialize: function () {
this.voidType = new VoidType();
this.undefinedType = new UndefinedType();
this.nullType = new NullType();
this.booleanType = new BooleanType();
this.integerType = new IntegerType();
this.numberType = new NumberType();
this.stringType = new StringType();
this.variantType = new VariantType();
},
serialize: function () {
return this.toString();
},
isAssignable: null, // bool isAssignable()
isConvertibleTo: null, // bool isConvertibleTo(type)
getClassDef: null, // ClassDefinition getClassDef()
equals: function (x) {
return this == x || ((x instanceof Type) && this.toString() == x.toString());
},
resolveIfMayBeUndefined: function () {
if (this instanceof MayBeUndefinedType)
return this.getBaseType();
return this;
},
asAssignableType: function () {
return this;
},
toMayBeUndefinedType: function () {
if (this instanceof MayBeUndefinedType || this instanceof VariantType)
return this;
return new MayBeUndefinedType(this);
},
$templateTypeToString: function (parameterizedTypeName, typeArgs) {
var s = parameterizedTypeName + ".<";
for (var i = 0; i < typeArgs.length; ++i) {
if (i != 0)
s += ",";
s += typeArgs[i].toString();
}
s += ">";
return s;
},
$isIntegerOrNumber: function (type) {
return type instanceof IntegerType || type instanceof NumberType;
}
});
// void and null are special types
var VoidType = exports.VoidType = Type.extend({
instantiate: function (instantiationContext) {
return this;
},
isAssignable: function () {
return false;
},
isConvertibleTo: function (type) {
return false;
},
getClassDef: function () {
throw new Error("not supported");
},
toString: function () {
return "void";
}
});
var NullType = exports.NullType = Type.extend({
instantiate: function (instantiationContext) {
return this;
},
isAssignable: function () {
return false;
},
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof ObjectType || type instanceof VariantType || type instanceof StaticFunctionType;
},
getClassDef: function () {
throw new Error("not supported");
},
toString: function () {
return "null";
}
});
// primitive types
var PrimitiveType = exports.PrimitiveType = Type.extend({
instantiate: function (instantiationContext) {
return this;
},
isAssignable: function () {
return true; // still does not support "const" qualifier
}
});
var BooleanType = exports.BooleanType = PrimitiveType.extend({
$_classDef: null,
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof BooleanType || type instanceof VariantType;
},
getClassDef: function () {
return BooleanType._classDef;
},
toString: function () {
return "boolean";
}
});
var IntegerType = exports.IntegerType = PrimitiveType.extend({
$_classDef: null,
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof IntegerType || type instanceof NumberType || type instanceof VariantType;
},
getClassDef: function () {
return NumberType._classDef;
},
toString: function () {
return "int";
}
});
var NumberType = exports.NumberType = PrimitiveType.extend({
$_classDef: null,
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof IntegerType || type instanceof NumberType || type instanceof VariantType;
},
getClassDef: function () {
return NumberType._classDef;
},
toString: function () {
return "number";
}
});
var StringType = exports.StringType = PrimitiveType.extend({
$_classDef: null,
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof StringType || type instanceof VariantType;
},
getClassDef: function () {
return StringType._classDef;
},
toString: function () {
return "string";
}
});
// any type
var VariantType = exports.VariantType = Type.extend({
instantiate: function (instantiationContext) {
return this;
},
isAssignable: function () {
return true;
},
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
return type instanceof VariantType;
},
getClassDef: function () {
throw new Error("not supported");
},
toString: function () {
return "variant";
}
});
// undefined and MayBeUndefined
var UndefinedType = exports.UndefinedType = Type.extend({
instantiate: function (instantiationContext) {
return this;
},
isAssignable: function () {
return false;
},
isConvertibleTo: function (type) {
return type instanceof MayBeUndefinedType || type instanceof VariantType;
},
getClassDef: function () {
throw new Error("not supported");
},
toString: function () {
return "undefined";
}
});
var MayBeUndefinedType = exports.MayBeUndefinedType = Type.extend({
constructor: function (type) {
if (type.equals(Type.variantType))
throw new Error("logic error, cannot create MayBeUndefined.<variant>");
this._baseType = type instanceof MayBeUndefinedType ? type._baseType : type;
},
instantiate: function (instantiationContext) {
var baseType = this._baseType.instantiate(instantiationContext);
return baseType.toMayBeUndefinedType();
},
isConvertibleTo: function (type) {
return this._baseType.isConvertibleTo(type instanceof MayBeUndefinedType ? type._baseType : type);
},
isAssignable: function () {
return true;
},
getClassDef: function () {
return this._baseType.getClassDef();
},
getBaseType: function () {
return this._baseType;
},
toString: function () {
return "MayBeUndefined.<" + this._baseType.toString() + ">";
}
});
// class and object types
var ClassDefType = exports.ClassDefType = Type.extend({
constructor: function (classDef) {
this._classDef = classDef;
},
instantiate: function (instantiationContext) {
throw new Error("logic flaw; ClassDefType is created during semantic analysis, after template instantiation");
},
isConvertibleTo: function (type) {
return false;
},
isAssignable: function () {
return false;
},
getClassDef: function () {
return this._classDef;
},
toString: function () {
return this._classDef.className();
}
});
var ObjectType = exports.ObjectType = Type.extend({
constructor: function (classDef) {
this._classDef = classDef;
},
instantiate: function (instantiationContext) {
throw new Error("logic flaw; ObjectType is created during semantic analysis, after template instantiation");
},
resolveType: function (context) {
if (this._classDef == null)
throw new Error("logic flaw");
},
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
if (type instanceof VariantType)
return true;
// conversions from Number / String to number / string is handled in each operator (since the behavior differ bet. the operators)
if (! (type instanceof ObjectType))
return false;
return this._classDef.isConvertibleTo(type._classDef);
},
isAssignable: function () {
return true; // still does not support "const" qualifier
},
getClassDef: function () {
return this._classDef;
},
toString: function () {
return this._classDef.className();
}
});
var ParsedObjectType = exports.ParsedObjectType = ObjectType.extend({
constructor: function (qualifiedName, typeArgs) {
ObjectType.prototype.constructor.call(this, null);
this._qualifiedName = qualifiedName;
this._typeArguments = typeArgs;
},
instantiate: function (instantiationContext) {
if (this._typeArguments.length == 0) {
var actualType = instantiationContext.typemap[this._qualifiedName.getToken().getValue()];
if (actualType != undefined)
return actualType;
}
var typeArgs = [];
for (var i = 0; i < this._typeArguments.length; ++i) {
var actualType = instantiationContext.typemap[this._typeArguments[i].toString()];
typeArgs[i] = actualType != undefined ? actualType : this._typeArguments[i];
}
var objectType = new ParsedObjectType(this._qualifiedName, typeArgs);
instantiationContext.objectTypesUsed.push(objectType);
return objectType;
},
resolveType: function (context) {
if (this._classDef == null) {
if (this._typeArguments.length == 0) {
this._classDef = this._qualifiedName.getClass(context);
} else {
// get the already-instantiated class (FIXME refactor, or should we move QualifiedName#getClass to somewhere else?)
if ((this._classDef = context.parser.lookup(context.errors, this._qualifiedName.getToken(), this.toString())) == null)
context.errors.push(new CompileError(this._token, "'" + this.toString() + "' is not defined"));
}
}
},
toString: function () {
return this._typeArguments.length != 0 ? Type.templateTypeToString(this._qualifiedName.getToken().getValue(), this._typeArguments) : this._qualifiedName.getToken().getValue();
}
});
// function types
var FunctionType = exports.FunctionType = Type.extend({
$_classDef: null,
getClassDef: function () {
return FunctionType._classDef;
}
});
var FunctionChoiceType = exports.FunctionChoiceType = FunctionType.extend({
constructor: function (types) {
this._types = types;
},
isAssignable: function () {
return false;
},
asAssignableType: function () {
throw new Error("logic flaw");
},
isConvertibleTo: function (type) {
return false;
},
deduceByArgumentTypes: function (context, operatorToken, argTypes, isStatic) {
// try an exact match
for (var i = 0; i < this._types.length; ++i)
if (this._types[i]._deduceByArgumentTypes(argTypes, isStatic, true))
return this._types[i];
// try loose match
var matched = [];
for (var i = 0; i < this._types.length; ++i)
if (this._types[i]._deduceByArgumentTypes(argTypes, isStatic, false))
matched.push(this._types[i]);
switch (matched.length) {
case 0:
context.errors.push(new CompileError(operatorToken, "no function with matching arguments"));
break;
case 1:
return matched[0];
default:
context.errors.push(new CompileError(operatorToken, "result of function resolution using the arguments is ambiguous"));
break;
}
return null;
},
toString: function () {
return this._types.length == 1 ? this._types[0].toString() : "<<multiple choices>>";
}
});
var ResolvedFunctionType = exports.ResolvedFunctionType = FunctionType.extend({
constructor: function (returnType, argTypes, isAssignable) {
this._returnType = returnType;
this._argTypes = argTypes;
this._isAssignable = isAssignable;
},
setIsAssignable: function (isAssignable) {
this._isAssignable = isAssignable;
return this;
},
isAssignable: function () {
return this._isAssignable;
},
asAssignableType: function () {
return this._clone().setIsAssignable(true);
},
getReturnType: function () {
return this._returnType;
},
getArgumentTypes: function () {
return this._argTypes;
},
deduceByArgumentTypes: function (context, operatorToken, argTypes, isStatic) {
if (! this._deduceByArgumentTypes(argTypes, isStatic, false)) {
context.errors.push(new CompileError(operatorToken, "no function with matching arguments"));
return null;
}
return this;
},
_deduceByArgumentTypes: function (argTypes, isStatic, exact) {
if ((this instanceof StaticFunctionType) != isStatic)
return false;
if (this._argTypes.length != argTypes.length)
return false;
for (var i = 0; i < argTypes.length; i++) {
if (this._argTypes[i].equals(argTypes[i])) {
// ok
} else {
if (exact)
return false;
if (! argTypes[i].isConvertibleTo(this._argTypes[i]))
return false;
}
}
return true;
},
toString: function () {
var args = [];
for (var i = 0; i < this._argTypes.length; ++i)
args[i] = " : " + this._argTypes[i].toString();
return this._toStringPrefix() + "function (" + args.join(", ") + ") : " + this._returnType.toString();
}
});
var StaticFunctionType = exports.StaticFunctionType = ResolvedFunctionType.extend({
constructor: function (returnType, argTypes, isAssignable) {
ResolvedFunctionType.prototype.constructor.call(this, returnType, argTypes, isAssignable);
},
instantiate: function (instantiationContext) {
var returnType = this._returnType.instantiate(instantiationContext);
if (returnType == null)
return null;
var argTypes = [];
for (var i = 0; i < this._argTypes.length; ++i)
if ((argTypes[i] = this._argTypes[i].instantiate(instantiationContext)) == null)
return null;
return new StaticFunctionType(returnType, argTypes, this._isAssignable);
},
_clone: function () {
return new StaticFunctionType(this._returnType, this._argTypes, this._isAssignable);
},
isConvertibleTo: function (type) {
type = type.resolveIfMayBeUndefined();
if (type instanceof VariantType)
return true;
if (! (type instanceof StaticFunctionType))
return false;
if (! this._returnType.equals(type.getReturnType()))
return false;
return this._deduceByArgumentTypes(type.getArgumentTypes(), true, true);
},
_toStringPrefix: function () {
return "";
}
});
var MemberFunctionType = exports.MemberFunctionType = ResolvedFunctionType.extend({
constructor: function (objectType, returnType, argTypes, isAssignable) {
ResolvedFunctionType.prototype.constructor.call(this, returnType, argTypes, isAssignable);
this._objectType = objectType;
},
_clone: function () {
return new MemberFunctionType(this._objectType, this._returnType, this._argTypes, this._isAssignable);
},
_toStringPrefix: function () {
return this._objectType.toString() + ".";
},
getObjectType: function () {
return this._objectType;
}
});
Type._initialize();