-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NTIScope.java
745 lines (672 loc) · 24.5 KB
/
NTIScope.java
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* Copyright 2013 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.newtypes.Declaration;
import com.google.javascript.jscomp.newtypes.DeclaredFunctionType;
import com.google.javascript.jscomp.newtypes.DeclaredTypeRegistry;
import com.google.javascript.jscomp.newtypes.EnumType;
import com.google.javascript.jscomp.newtypes.FunctionNamespace;
import com.google.javascript.jscomp.newtypes.JSType;
import com.google.javascript.jscomp.newtypes.JSTypeCreatorFromJSDoc;
import com.google.javascript.jscomp.newtypes.JSTypes;
import com.google.javascript.jscomp.newtypes.Namespace;
import com.google.javascript.jscomp.newtypes.NamespaceLit;
import com.google.javascript.jscomp.newtypes.QualifiedName;
import com.google.javascript.jscomp.newtypes.RawNominalType;
import com.google.javascript.jscomp.newtypes.Typedef;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author blickly@google.com (Ben Lickly)
* @author dimvar@google.com (Dimitris Vardoulakis)
*/
final class NTIScope implements DeclaredTypeRegistry {
private final NTIScope parent;
private final Node root;
// Name on the function AST node; null for top scope & anonymous functions
private final String name;
private final JSTypes commonTypes;
// Becomes true after finalizeScope is run; so it's true during NTI.
private boolean isFinalized = false;
// A local w/out declared type is mapped to null, not to JSType.UNKNOWN.
private final Map<String, JSType> locals = new LinkedHashMap<>();
private final Map<String, JSType> externs;
private final Set<String> constVars = new LinkedHashSet<>();
private final List<String> formals;
// Variables that are defined in this scope and used in inner scopes.
private Set<String> escapedVars = new LinkedHashSet<>();
// outerVars are the variables that appear free in this scope
// and are defined in an outer scope.
private final Set<String> outerVars = new LinkedHashSet<>();
// When a function is also used as a namespace, we add entries to both
// localFunDefs and localNamespaces. After finalizeScope (when NTI runs),
// the function has an entry in localFunDefs, and in locals or externs.
private final Map<String, NTIScope> localFunDefs = new LinkedHashMap<>();
private ImmutableSet<String> unknownTypeNames = ImmutableSet.of();
private Map<String, Typedef> localTypedefs = new LinkedHashMap<>();
private Map<String, Namespace> localNamespaces = new LinkedHashMap<>();
// The namespace map that we preserve post-finalization, purely for use
// in GlobalTypeInfo for symbol table purposes.
private Map<String, Namespace> preservedNamespaces;
// The set localEnums is used for enum resolution, and then discarded.
private Set<EnumType> localEnums = new LinkedHashSet<>();
// declaredType is null for top level, but never null for functions,
// even those without jsdoc.
// Any inferred parameters or return will be set to null individually.
private DeclaredFunctionType declaredType;
NTIScope(Node root, NTIScope parent, List<String> formals, JSTypes commonTypes) {
if (parent == null) {
this.name = null;
this.externs = new LinkedHashMap<>();
} else {
String nameOnAst = root.getFirstChild().getString();
this.name = nameOnAst.isEmpty() ? null : nameOnAst;
this.externs = ImmutableMap.of();
}
this.root = root;
this.parent = parent;
this.formals = formals;
this.commonTypes = commonTypes;
}
Node getRoot() {
return this.root;
}
NTIScope getParent() {
return this.parent;
}
Node getBody() {
Preconditions.checkArgument(root.isFunction());
return NodeUtil.getFunctionBody(root);
}
/** Used only for error messages; null for top scope */
String getReadableName() {
// TODO(dimvar): don't return null for anonymous functions
return isTopLevel() ? null : NodeUtil.getName(root);
}
String getName() {
return name;
}
void setDeclaredType(DeclaredFunctionType declaredType) {
Preconditions.checkNotNull(declaredType);
this.declaredType = declaredType;
// In NTI, we set the type of a function node after we create the summary.
// NTI doesn't analyze externs, so we set the type for extern functions here.
if (this.root.isFromExterns()) {
this.root.setTypeI(getCommonTypes().fromFunctionType(declaredType.toFunctionType()));
}
}
@Override
public DeclaredFunctionType getDeclaredFunctionType() {
return this.declaredType;
}
boolean isFunction() {
return root.isFunction();
}
boolean isTopLevel() {
return parent == null;
}
boolean isConstructor() {
if (!root.isFunction()) {
return false;
}
JSDocInfo fnDoc = NodeUtil.getBestJSDocInfo(root);
return fnDoc != null && fnDoc.isConstructor();
}
boolean isPrototypeMethod() {
Preconditions.checkArgument(root != null);
return NodeUtil.isPrototypeMethod(root);
}
void addUnknownTypeNames(Set<String> names) {
// TODO(dimvar): if sm uses a goog.forwardDeclare in a local scope, give
// an error instead of crashing.
Preconditions.checkState(this.isTopLevel());
this.unknownTypeNames = ImmutableSet.copyOf(names);
}
void addLocalFunDef(String name, NTIScope scope) {
Preconditions.checkArgument(!name.isEmpty());
Preconditions.checkArgument(!name.contains("."));
Preconditions.checkArgument(!isDefinedLocally(name, false));
localFunDefs.put(name, scope);
}
boolean isFormalParam(String name) {
return formals.contains(name);
}
boolean isLocalFunDef(String name) {
return localFunDefs.containsKey(name);
}
boolean isFunctionNamespace(String name) {
Preconditions.checkArgument(!name.contains("."));
Preconditions.checkState(isFinalized);
Declaration d = getDeclaration(name, false);
if (d == null || d.getFunctionScope() == null || d.getTypeOfSimpleDecl() == null) {
return false;
}
return d.getTypeOfSimpleDecl().isNamespace();
}
// In other languages, type names and variable names are in distinct
// namespaces and don't clash.
// But because our typedefs and enums are var declarations, they are in the
// same namespace as other variables.
boolean isDefinedLocally(String name, boolean includeTypes) {
Preconditions.checkNotNull(name);
Preconditions.checkState(!name.contains("."));
if (locals.containsKey(name)
|| formals.contains(name)
|| localNamespaces.containsKey(name)
|| localFunDefs.containsKey(name)
|| "this".equals(name)
|| externs.containsKey(name)
|| localTypedefs.containsKey(name)) {
return true;
}
if (includeTypes) {
return unknownTypeNames.contains(name)
|| declaredType != null && declaredType.isTypeVariableDefinedLocally(name);
}
return false;
}
// For variables it is the same as isDefinedLocally; for properties it looks
// for a definition in any scope.
boolean isDefined(Node qnameNode) {
Preconditions.checkArgument(qnameNode.isQualifiedName());
if (qnameNode.isName()) {
return isDefinedLocally(qnameNode.getString(), false);
} else if (qnameNode.isThis()) {
return true;
}
QualifiedName qname = QualifiedName.fromNode(qnameNode);
String leftmost = qname.getLeftmostName();
if (isNamespace(leftmost)) {
return getNamespace(leftmost).isDefined(qname.getAllButLeftmost());
}
return parent == null ? false : parent.isDefined(qnameNode);
}
boolean isNamespace(Node expr) {
if (expr.isName()) {
return isNamespace(expr.getString());
}
if (!expr.isGetProp()) {
return false;
}
return isNamespace(QualifiedName.fromNode(expr));
}
boolean isNamespace(QualifiedName qname) {
if (qname == null) {
return false;
}
String leftmost = qname.getLeftmostName();
return isNamespace(leftmost)
&& (qname.isIdentifier()
|| getNamespace(leftmost).hasSubnamespace(qname.getAllButLeftmost()));
}
boolean isNamespace(String name) {
Preconditions.checkArgument(!name.contains("."));
Declaration decl = getDeclaration(name, false);
if (decl == null) {
return false;
}
JSType simpleType = decl.getTypeOfSimpleDecl();
return decl.getNamespace() != null
|| simpleType != null && simpleType.isNamespace();
}
boolean isVisibleInScope(String name) {
Preconditions.checkArgument(!name.contains("."));
return isDefinedLocally(name, false)
|| name.equals(this.name)
|| (parent != null && parent.isVisibleInScope(name));
}
boolean isConstVar(String name) {
Preconditions.checkArgument(!name.contains("."));
Declaration decl = getDeclaration(name, false);
return decl != null && decl.isConstant();
}
boolean isOuterVarEarly(String name) {
Preconditions.checkArgument(!name.contains("."));
return !isDefinedLocally(name, false)
&& parent != null && parent.isVisibleInScope(name);
}
boolean isGlobalVar(String varName) {
NTIScope s = this;
while (s.parent != null) {
if (isDefinedLocally(varName, false)) {
return false;
}
s = s.parent;
}
return true;
}
boolean isUndeclaredFormal(String name) {
Preconditions.checkArgument(!name.contains("."));
return formals.contains(name) && getDeclaredTypeOf(name) == null;
}
List<String> getFormals() {
return new ArrayList<>(formals);
}
Set<String> getOuterVars() {
return new LinkedHashSet<>(outerVars);
}
Set<String> getLocalFunDefs() {
return ImmutableSet.copyOf(localFunDefs.keySet());
}
boolean isOuterVar(String name) {
return outerVars.contains(name);
}
boolean isUndeclaredOuterVar(String name) {
return outerVars.contains(name) && getDeclaredTypeOf(name) == null;
}
boolean isEscapedVar(String name) {
return this.escapedVars.contains(name);
}
boolean hasThis() {
if (!isFunction()) {
return false;
}
DeclaredFunctionType dft = getDeclaredFunctionType();
// dft is null for function scopes early during GlobalTypeInfo
return dft != null && dft.getThisType() != null;
}
@Override
public JSTypes getCommonTypes() {
if (isTopLevel()) {
return commonTypes;
}
return parent.getCommonTypes();
}
@Override
public JSType getDeclaredTypeOf(String name) {
Preconditions.checkArgument(!name.contains("."));
if ("this".equals(name)) {
if (!hasThis()) {
return null;
}
return getDeclaredFunctionType().getThisType();
}
Declaration decl = getLocalDeclaration(name, false);
if (decl != null) {
if (decl.getTypeOfSimpleDecl() != null) {
Preconditions.checkState(!decl.getTypeOfSimpleDecl().isBottom(),
"%s was bottom", name);
return decl.getTypeOfSimpleDecl();
}
NTIScope funScope = (NTIScope) decl.getFunctionScope();
if (funScope != null) {
Preconditions.checkNotNull(
funScope.getDeclaredFunctionType(),
"decl=%s, funScope=%s", decl, funScope);
return getCommonTypes().fromFunctionType(
funScope.getDeclaredFunctionType().toFunctionType());
}
Preconditions.checkState(decl.getNamespace() == null);
return null;
}
// When a function is a namespace, the parent scope has a better type.
if (name.equals(this.name) && !parent.isFunctionNamespace(name)) {
return getCommonTypes()
.fromFunctionType(getDeclaredFunctionType().toFunctionType());
}
if (parent != null) {
return parent.getDeclaredTypeOf(name);
}
return null;
}
boolean hasUndeclaredFormalsOrOuters() {
for (String formal : formals) {
if (getDeclaredTypeOf(formal) == null) {
return true;
}
}
for (String outer : outerVars) {
JSType declType = getDeclaredTypeOf(outer);
if (declType == null
// Undeclared functions have a non-null declared type,
// but they always have a return type of unknown
|| (declType.getFunType() != null
&& declType.getFunType().getReturnType().isUnknown())) {
return true;
}
}
return false;
}
private NTIScope getScopeHelper(QualifiedName qname) {
Declaration decl = getDeclaration(qname, false);
return decl == null ? null : (NTIScope) decl.getFunctionScope();
}
boolean isKnownFunction(String fnName) {
Preconditions.checkArgument(!fnName.contains("."));
return getScopeHelper(new QualifiedName(fnName)) != null;
}
boolean isKnownFunction(QualifiedName qname) {
return getScopeHelper(qname) != null;
}
boolean isExternalFunction(String fnName) {
NTIScope s = getScopeHelper(new QualifiedName(fnName));
return s.root.isFromExterns();
}
NTIScope getScope(String fnName) {
NTIScope s = getScopeHelper(new QualifiedName(fnName));
Preconditions.checkState(s != null);
return s;
}
Set<String> getLocals() {
return ImmutableSet.copyOf(locals.keySet());
}
Set<String> getExterns() {
return ImmutableSet.copyOf(externs.keySet());
}
// We don't check for duplicates here, mainly because we add some
// intentionally during the two phases of GlobalTypeInfo.
// If a variable is declared many times in a scope, the last definition
// overwrites the previous ones. For correctness, we rely on the fact that
// the var-check passes run before type checking.
void addLocal(String name, JSType declType,
boolean isConstant, boolean isFromExterns) {
Preconditions.checkArgument(!name.contains("."));
if (isConstant) {
constVars.add(name);
}
if (isFromExterns) {
externs.put(name, declType);
} else {
locals.put(name, declType);
}
}
static void mayRecordEscapedVar(NTIScope s, String name) {
if (s.isDefinedLocally(name, false)) {
return;
}
while (s != null) {
if (s.isDefinedLocally(name, false)) {
s.escapedVars.add(name);
return;
}
s = s.parent;
}
}
RawNominalType getNominalType(QualifiedName qname) {
Declaration decl = getDeclaration(qname, false);
return decl == null ? null : decl.getNominal();
}
Typedef getTypedef(String name) {
Preconditions.checkState(!name.contains("."));
Declaration decl = getDeclaration(name, false);
return decl == null ? null : decl.getTypedef();
}
EnumType getEnum(QualifiedName qname) {
Declaration decl = getDeclaration(qname, false);
return decl == null ? null : decl.getEnum();
}
Namespace getNamespace(String name) {
Preconditions.checkArgument(!name.contains("."));
Declaration decl = getDeclaration(name, false);
return decl == null ? null : decl.getNamespace();
}
void addFunNamespace(Node qnameNode) {
if (qnameNode.isName()) {
String varName = qnameNode.getString();
Preconditions.checkArgument(isDefinedLocally(varName, false));
Preconditions.checkState(!this.localNamespaces.containsKey(varName));
NTIScope s = Preconditions.checkNotNull(this.localFunDefs.get(varName));
this.localNamespaces.put(varName,
new FunctionNamespace(getCommonTypes(), varName, s, qnameNode));
} else {
Preconditions.checkArgument(!isNamespace(qnameNode));
QualifiedName qname = QualifiedName.fromNode(qnameNode);
Namespace ns = getNamespace(qname.getLeftmostName());
NTIScope s = (NTIScope) ns.getDeclaration(qname).getFunctionScope();
ns.addNamespace(qname.getAllButLeftmost(),
new FunctionNamespace(getCommonTypes(), qname.toString(), s, qnameNode));
}
}
void addNamespaceLit(Node qnameNode) {
addNamespace(qnameNode,
new NamespaceLit(getCommonTypes(), qnameNode.getQualifiedName(), qnameNode));
}
void updateType(String name, JSType newDeclType) {
if (isDefinedLocally(name, false)) {
locals.put(name, newDeclType);
} else if (parent != null) {
parent.updateType(name, newDeclType);
} else {
throw new RuntimeException(
"Cannot update type of unknown variable: " + name);
}
}
void addOuterVar(String name) {
outerVars.add(name);
}
void addTypedef(Node qnameNode, Typedef td) {
if (qnameNode.isName()) {
Preconditions.checkState(
!localTypedefs.containsKey(qnameNode.getString()));
localTypedefs.put(qnameNode.getString(), td);
} else {
Preconditions.checkState(!isDefined(qnameNode));
QualifiedName qname = QualifiedName.fromNode(qnameNode);
Namespace ns = getNamespace(qname.getLeftmostName());
ns.addTypedef(qname.getAllButLeftmost(), td);
}
}
void addNamespace(Node qnameNode, Namespace ns) {
if (ns instanceof EnumType) {
this.localEnums.add((EnumType) ns);
}
if (qnameNode.isName()) {
String varName = qnameNode.getString();
Preconditions.checkState(!this.localNamespaces.containsKey(varName));
this.localNamespaces.put(varName, ns);
if (qnameNode.isFromExterns() && !this.externs.containsKey(varName)) {
// We don't know the full type of a namespace until after we see all
// its properties. But we want to add it to the externs, otherwise it
// is treated as a local and initialized to the wrong thing in NTI.
this.externs.put(qnameNode.getString(), null);
}
} else {
Preconditions.checkState(!isDefined(qnameNode));
QualifiedName qname = QualifiedName.fromNode(qnameNode);
Namespace rootns = getNamespace(qname.getLeftmostName());
rootns.addNamespace(qname.getAllButLeftmost(), ns);
}
}
Namespace getNamespace(QualifiedName qname) {
Namespace ns = getNamespace(qname.getLeftmostName());
return (ns == null || qname.isIdentifier())
? ns : ns.getSubnamespace(qname.getAllButLeftmost());
}
private Declaration getLocalDeclaration(String name, boolean includeTypes) {
Preconditions.checkArgument(!name.contains("."));
if (!isDefinedLocally(name, includeTypes)) {
return null;
}
JSType type = null;
boolean isTypeVar = false;
if ("this".equals(name)) {
type = getDeclaredTypeOf("this");
} else if (locals.containsKey(name)) {
type = locals.get(name);
} else if (formals.contains(name)) {
int formalIndex = formals.indexOf(name);
if (declaredType != null && formalIndex != -1) {
JSType formalType = declaredType.getFormalType(formalIndex);
if (formalType != null && !formalType.isBottom()) {
type = formalType;
}
}
} else if (localFunDefs.containsKey(name)) {
// After finalization, the externs contain the correct type for
// external function namespaces, don't rely on localFunDefs
if (isFinalized && externs.containsKey(name)) {
type = externs.get(name);
}
} else if (localTypedefs.containsKey(name) || localNamespaces.containsKey(name)) {
// Any further declarations are shadowed
} else if (declaredType != null && declaredType.isTypeVariableDefinedLocally(name)) {
isTypeVar = true;
type = JSType.fromTypeVar(declaredType.getTypeVariableDefinedLocally(name));
} else if (externs.containsKey(name)) {
type = externs.get(name);
}
Namespace ns = null;
if (localNamespaces.containsKey(name)) {
ns = localNamespaces.get(name);
} else if (preservedNamespaces != null) {
ns = preservedNamespaces.get(name);
}
return new Declaration(type, localTypedefs.get(name),
ns, localFunDefs.get(name), isTypeVar,
constVars.contains(name));
}
@Override
public Declaration getDeclaration(QualifiedName qname, boolean includeTypes) {
if (qname.isIdentifier()) {
return getDeclaration(qname.getLeftmostName(), includeTypes);
}
Preconditions.checkState(!this.isFinalized,
"Namespaces are removed from scopes after finalization");
Namespace ns = getNamespace(qname.getLeftmostName());
if (ns == null) {
return maybeGetForwardDeclaration(qname.toString());
}
Declaration decl = ns.getDeclaration(qname.getAllButLeftmost());
return decl != null ? decl : maybeGetForwardDeclaration(qname.toString());
}
private Declaration maybeGetForwardDeclaration(String qname) {
NTIScope globalScope = this;
while (globalScope.parent != null) {
globalScope = globalScope.parent;
}
if (globalScope.unknownTypeNames.contains(qname)) {
return new Declaration(JSType.UNKNOWN, null, null, null, false, false);
}
return null;
}
public Declaration getDeclaration(String name, boolean includeTypes) {
Preconditions.checkArgument(!name.contains("."));
Declaration decl = getLocalDeclaration(name, includeTypes);
if (decl != null) {
return decl;
}
return parent == null ? null : parent.getDeclaration(name, includeTypes);
}
public JSType getType(String typeName) {
Preconditions.checkNotNull(
preservedNamespaces, "Failed to preserve namespaces post-finalization");
Namespace ns = preservedNamespaces.get(typeName);
if (ns instanceof RawNominalType) {
return ((RawNominalType) ns).getInstanceAsJSType();
}
return null;
}
void resolveTypedefs(JSTypeCreatorFromJSDoc typeParser) {
for (Typedef td : localTypedefs.values()) {
if (!td.isResolved()) {
typeParser.resolveTypedef(td, this);
}
}
}
void resolveEnums(JSTypeCreatorFromJSDoc typeParser) {
for (EnumType e : localEnums) {
if (!e.isResolved()) {
typeParser.resolveEnum(e, this);
}
}
localEnums = null;
}
void finalizeScope() {
Preconditions.checkState(isTopLevel() || this.declaredType != null,
"No declared type for function-scope: %s", this.root);
unknownTypeNames = ImmutableSet.of();
// For now, we put types of namespaces directly into the locals.
// Alternatively, we could move this into NewTypeInference.initEdgeEnvs
for (Map.Entry<String, Namespace> entry : localNamespaces.entrySet()) {
String name = entry.getKey();
Namespace ns = entry.getValue();
JSType t;
if (ns instanceof NamespaceLit) {
constVars.add(name);
NamespaceLit nslit = (NamespaceLit) ns;
// The argument to maybeSetWindowInstance should only be non-null for
// window, but we don't check here to avoid hard-coding the name.
// Enforced in GlobalTypeInfo.
nslit.maybeSetWindowInstance(externs.get(name));
t = nslit.toJSType();
} else {
t = ns.toJSType();
}
if (externs.containsKey(name)) {
externs.put(name, t);
} else {
locals.put(name, t);
}
}
for (String typedefName : localTypedefs.keySet()) {
locals.put(typedefName, JSType.UNDEFINED);
}
copyOuterVarsTransitively(this);
preservedNamespaces = localNamespaces;
localNamespaces = ImmutableMap.of();
localTypedefs = ImmutableMap.of();
escapedVars = ImmutableSet.of();
isFinalized = true;
}
// A scope must know about the free variables used in outer scopes,
// otherwise we end up with invalid type envs.
private static void copyOuterVarsTransitively(NTIScope s) {
if (s.isTopLevel()) {
return;
}
NTIScope parent = s.parent;
Set<String> outerVars = s.outerVars;
while (parent.isFunction()) {
boolean copiedOneVar = false;
for (String v : outerVars) {
if (!parent.isDefinedLocally(v, false)) {
copiedOneVar = true;
parent.addOuterVar(v);
}
}
if (!copiedOneVar) {
break;
}
outerVars = parent.outerVars;
parent = parent.parent;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (isTopLevel()) {
sb.append("<TOP SCOPE>");
} else {
sb.append(getReadableName());
sb.append('(');
Joiner.on(',').appendTo(sb, formals);
sb.append(')');
}
sb.append(" with root: ");
sb.append(root);
return sb.toString();
}
}