forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullDecls.ts
More file actions
497 lines (398 loc) · 18 KB
/
Copy pathpullDecls.ts
File metadata and controls
497 lines (398 loc) · 18 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
// Copyright (c) Microsoft. All rights reserved. Licensed under the Apache License, Version 2.0.
// See LICENSE.txt in the project root for complete license information.
///<reference path='..\references.ts' />
module TypeScript {
var pullDeclID = 0;
var sentinelEmptyPullDeclArray: any[] = [];
export interface IASTForDeclMap {
_getASTForDecl(decl: PullDecl): ISyntaxElement;
}
export class PullDecl {
// Properties that will not change over the lifetime of the decl
public kind: PullElementKind;
public name: string;
private declDisplayName: string;
public declID = pullDeclID++;
public flags: PullElementFlags = PullElementFlags.None;
private declGroups: IIndexable<PullDeclGroup> = null;
// Child decls
private childDecls: PullDecl[] = null;
private typeParameters: PullDecl[] = null;
// In the case of classes, initialized modules and enums, we need to track the implicit
// value set to the constructor or instance type. We can use this field to make sure that on
// edits and updates we don't leak the val decl or symbol
private synthesizedValDecl: PullDecl = null;
// backreference from the value decl back to non-value part
private containerDecl: PullDecl = null;
// Caches
// Mappings from names to decls. Public only for diffing purposes.
public childDeclTypeCache: IIndexable<PullDecl[]> = null;
public childDeclValueCache: IIndexable<PullDecl[]> = null;
public childDeclNamespaceCache: IIndexable<PullDecl[]> = null;
public childDeclTypeParameterCache: IIndexable<PullDecl[]> = null;
constructor(declName: string, displayName: string, kind: PullElementKind, declFlags: PullElementFlags) {
this.name = declName;
this.kind = kind;
this.flags = declFlags;
if (displayName !== this.name) {
this.declDisplayName = displayName;
}
}
public getASTForDeclMap(): IASTForDeclMap {
throw Errors.abstract();
}
public fileName(): string {
throw Errors.abstract();
}
public getParentPath(): PullDecl[] {
throw Errors.abstract();
}
public getParentDecl(): PullDecl {
throw Errors.abstract();
}
public isExternalModule(): boolean {
throw Errors.abstract();
}
// The enclosing decl is different from the parent decl. It is a syntactic ancestor decl
// that introduces the scope in which this decl is defined.
// Call this to get the enclosing decl of *this* decl
public getEnclosingDecl(): PullDecl {
throw Errors.abstract();
}
// Note: Call this on the *parent* decl of the decl whose enclosing decl you want.
// This will assume you have already skipped the current decl on your way to the
// enclosing decl.
public _getEnclosingDeclFromParentDecl(): PullDecl {
// Skip over the decls that cannot introduce a scope
var decl = this;
while (decl) {
switch (decl.kind) {
default:
return decl;
case PullElementKind.Variable:
case PullElementKind.TypeParameter:
case PullElementKind.Parameter:
case PullElementKind.TypeAlias:
case PullElementKind.EnumMember:
}
decl = decl.getParentDecl();
}
Debug.fail();
}
/** Use getName for type checking purposes, and getDisplayName to report an error or display info to the user.
* They will differ when the identifier is an escaped unicode character or the identifier "__proto__".
*/
public getDisplayName() {
return this.declDisplayName === undefined ? this.name : this.declDisplayName;
}
public setSymbol(symbol: PullSymbol, semanticInfoChain: SemanticInfoChain): void {
semanticInfoChain.setSymbolForDecl(this, symbol);
}
public ensureSymbolIsBound(semanticInfoChain: SemanticInfoChain) {
if (!this.hasBeenBound(semanticInfoChain) && this.kind !== PullElementKind.Script) {
var binder = semanticInfoChain.getBinder();
binder.bindDeclToPullSymbol(this);
}
}
public getSymbol(semanticInfoChain: SemanticInfoChain): PullSymbol {
if (this.kind === PullElementKind.Script) {
return null;
}
this.ensureSymbolIsBound(semanticInfoChain);
return semanticInfoChain.getSymbolForDecl(this);
}
public hasSymbol(semanticInfoChain: SemanticInfoChain) {
var symbol = semanticInfoChain.getSymbolForDecl(this);
return !!symbol;
}
public setSignatureSymbol(signatureSymbol: PullSignatureSymbol, semanticInfoChain: SemanticInfoChain): void {
semanticInfoChain.setSignatureSymbolForDecl(this, signatureSymbol);
}
public getSignatureSymbol(semanticInfoChain: SemanticInfoChain): PullSignatureSymbol {
this.ensureSymbolIsBound(semanticInfoChain);
return semanticInfoChain.getSignatureSymbolForDecl(this);
}
public hasSignatureSymbol(semanticInfoChain: SemanticInfoChain) {
var signatureSymbol = semanticInfoChain.getSignatureSymbolForDecl(this);
return !!signatureSymbol;
}
public setFlags(flags: PullElementFlags) { this.flags = flags; }
public setFlag(flags: PullElementFlags) { this.flags |= flags; }
public setValueDecl(valDecl: PullDecl) {
this.synthesizedValDecl = valDecl;
valDecl.containerDecl = this;
}
public getValueDecl() { return this.synthesizedValDecl; }
public getContainerDecl() { return this.containerDecl; }
private getChildDeclCache(declKind: PullElementKind): IIndexable<PullDecl[]> {
if (declKind === PullElementKind.TypeParameter) {
if (!this.childDeclTypeParameterCache) {
this.childDeclTypeParameterCache = createIntrinsicsObject<PullDecl[]>();
}
return this.childDeclTypeParameterCache;
}
else if (hasFlag(declKind, PullElementKind.SomeContainer)) {
if (!this.childDeclNamespaceCache) {
this.childDeclNamespaceCache = createIntrinsicsObject<PullDecl[]>();
}
return this.childDeclNamespaceCache;
}
else if (hasFlag(declKind, PullElementKind.SomeType)) {
if (!this.childDeclTypeCache) {
this.childDeclTypeCache = createIntrinsicsObject<PullDecl[]>();
}
return this.childDeclTypeCache;
}
else {
if (!this.childDeclValueCache) {
this.childDeclValueCache = createIntrinsicsObject<PullDecl[]>();
}
return this.childDeclValueCache;
}
}
// Should only be called by subclasses.
public addChildDecl(childDecl: PullDecl): void {
if (childDecl.kind === PullElementKind.TypeParameter) {
if (!this.typeParameters) {
this.typeParameters = [];
}
this.typeParameters[this.typeParameters.length] = childDecl;
}
else {
if (!this.childDecls) {
this.childDecls = [];
}
this.childDecls[this.childDecls.length] = childDecl;
}
// add to the appropriate cache
var declName = childDecl.name;
if (!(childDecl.kind & PullElementKind.SomeSignature)) {
var cache = this.getChildDeclCache(childDecl.kind);
var childrenOfName = cache[declName];
if (!childrenOfName) {
childrenOfName = [];
}
childrenOfName.push(childDecl);
cache[declName] = childrenOfName;
}
}
// Search for a child decl with the given name. 'isType' is used to specify whether or
// not child types or child values are returned.
public searchChildDecls(declName: string, searchKind: PullElementKind): PullDecl[]{
// find the decl with the optional type
// if necessary, cache the decl
// may be wise to return a chain of decls, or take a parent decl as a parameter
var cacheVal: PullDecl[] = null;
if (searchKind & PullElementKind.SomeType) {
cacheVal = this.childDeclTypeCache ? this.childDeclTypeCache[declName] : null;
}
else if (searchKind & PullElementKind.SomeContainer) {
cacheVal = this.childDeclNamespaceCache ? this.childDeclNamespaceCache[declName] : null;
}
else {
cacheVal = this.childDeclValueCache ? this.childDeclValueCache[declName] : null;
}
if (cacheVal) {
return cacheVal;
}
else {
// If we didn't find it, and they were searching for types, then also check the
// type parameter cache.
if (searchKind & PullElementKind.SomeType) {
cacheVal = this.childDeclTypeParameterCache ? this.childDeclTypeParameterCache[declName] : null;
if (cacheVal) {
return cacheVal;
}
}
return sentinelEmptyPullDeclArray;
}
}
public getChildDecls(): PullDecl[] {
return this.childDecls || sentinelEmptyPullDeclArray;
}
public getTypeParameters(): PullDecl[] {
return this.typeParameters || sentinelEmptyPullDeclArray;
}
public addVariableDeclToGroup(decl: PullDecl) {
if (!this.declGroups) {
this.declGroups = createIntrinsicsObject<PullDeclGroup>();
}
var declGroup = this.declGroups[decl.name];
if (declGroup) {
declGroup.addDecl(decl);
}
else {
declGroup = new PullDeclGroup(decl.name);
declGroup.addDecl(decl);
this.declGroups[decl.name] = declGroup;
}
}
public getVariableDeclGroups(): PullDecl[][] {
var declGroups: PullDecl[][] = null;
if (this.declGroups) {
for (var declName in this.declGroups) {
if (this.declGroups[declName]) {
if (declGroups === null) {
declGroups = [];
}
declGroups.push(this.declGroups[declName].getDecls());
}
}
}
return declGroups || sentinelEmptyPullDeclArray;
}
public hasBeenBound(semanticInfoChain: SemanticInfoChain) {
return this.hasSymbol(semanticInfoChain) || this.hasSignatureSymbol(semanticInfoChain);
}
public isSynthesized(): boolean {
return false;
}
public ast(): ISyntaxElement {
return this.isSynthesized() ? null : this.getASTForDeclMap()._getASTForDecl(this);
}
public isRootDecl() {
throw Errors.abstract();
}
}
// A root decl represents the top level decl for a file. By specializing this decl, we
// provide a location, per file, to store data that all decls in the file would otherwise
// have to duplicate. For example, there is no need to store the 'fileName' in each decl.
// Instead, only the root decl needs to store this data. Decls underneath it can determine
// the file name by queryign their parent. In other words, a Root Decl allows us to trade
// space for logarithmic speed.
export class RootPullDecl extends PullDecl {
private _isExternalModule: boolean;
private _fileName: string;
constructor(private astToDeclMap: IASTForDeclMap, name: string, fileName: string, kind: PullElementKind, declFlags: PullElementFlags, isExternalModule: boolean) {
super(name, name, kind, declFlags);
this._isExternalModule = isExternalModule;
this._fileName = fileName;
}
public fileName(): string {
return this._fileName;
}
public getASTForDeclMap(): IASTForDeclMap {
return this.astToDeclMap;
}
public getParentPath(): PullDecl[]{
return [this];
}
public getParentDecl(): PullDecl {
return null;
}
public isExternalModule(): boolean {
return this._isExternalModule;
}
// We never want the enclosing decl to be null, so if we are at a top level decl, return it
public getEnclosingDecl() {
return this;
}
public isRootDecl() {
return true;
}
}
export class NormalPullDecl extends PullDecl {
private parentDecl: PullDecl = null;
public _rootDecl: RootPullDecl;
private parentPath: PullDecl[] = null;
constructor(declName: string, displayName: string, kind: PullElementKind, declFlags: PullElementFlags, parentDecl: PullDecl, addToParent = true) {
super(declName, displayName, kind, declFlags);
// Link to parent
this.parentDecl = parentDecl;
if (addToParent) {
parentDecl.addChildDecl(this);
}
if (this.parentDecl) {
if (this.parentDecl.isRootDecl()) {
this._rootDecl = <RootPullDecl>this.parentDecl;
}
else {
this._rootDecl = (<NormalPullDecl>this.parentDecl)._rootDecl;
}
} else {
// Synthetic
Debug.assert(this.isSynthesized());
this._rootDecl = null;
}
//if (!parentDecl && !this.isSynthesized() && kind !== PullElementKind.Primitive) {
// throw Errors.invalidOperation("Orphaned decl " + PullElementKind[kind]);
//}
}
public fileName(): string {
return this._rootDecl.fileName();
}
public getASTForDeclMap(): IASTForDeclMap {
return this._rootDecl.getASTForDeclMap();
}
public getParentDecl(): PullDecl {
return this.parentDecl;
}
public getParentPath(): PullDecl[] {
if (!this.parentPath) {
var path: PullDecl[] = [this];
var parentDecl = this.parentDecl;
while (parentDecl) {
if (parentDecl && path[path.length - 1] !== parentDecl
&& !(parentDecl.kind & (PullElementKind.ObjectLiteral | PullElementKind.ObjectType))) {
path.unshift(parentDecl);
}
parentDecl = parentDecl.getParentDecl();
}
this.parentPath = path;
}
return this.parentPath;
}
public isExternalModule(): boolean {
return false;
}
public getEnclosingDecl(): PullDecl {
return this.parentDecl && this.parentDecl._getEnclosingDeclFromParentDecl();
}
public isRootDecl() {
return false;
}
}
export class PullEnumElementDecl extends NormalPullDecl {
public constantValue: number = null;
constructor(declName: string, displayName: string, parentDecl: PullDecl) {
super(declName, displayName, PullElementKind.EnumMember, PullElementFlags.Public, parentDecl);
}
}
export class PullFunctionExpressionDecl extends NormalPullDecl {
private functionExpressionName: string;
constructor(expressionName: string, declFlags: PullElementFlags, parentDecl: PullDecl, displayName: string = "") {
super("", displayName, PullElementKind.FunctionExpression, declFlags, parentDecl);
this.functionExpressionName = expressionName;
}
public getFunctionExpressionName(): string {
return this.functionExpressionName;
}
}
export class PullSynthesizedDecl extends NormalPullDecl {
// This is a synthesized decl; its life time should match that of the symbol using it, and
// not that of its parent decl. To enforce this we are not making it reachable from its
// parent, but will set the parent link.
constructor(declName: string, displayName: string, kind: PullElementKind, declFlags: PullElementFlags, parentDecl: PullDecl) {
super(declName, displayName, kind, declFlags, parentDecl, /*addToParent*/ false);
}
public isSynthesized(): boolean {
return true;
}
public fileName(): string {
return this._rootDecl ? this._rootDecl.fileName() : "";
}
}
export class PullDeclGroup {
private _decls: PullDecl[] = [];
constructor(public name: string) {
}
public addDecl(decl: PullDecl) {
if (decl.name === this.name) {
this._decls[this._decls.length] = decl;
}
}
public getDecls() {
return this._decls;
}
}
}