use of com.google.javascript.jscomp.newtypes.Declaration in project closure-compiler by google.
the class NTIScope method getDeclaration.
public Declaration getDeclaration(String name, boolean includeTypes) {
checkArgument(!name.contains("."));
Declaration decl = getLocalDeclaration(name, includeTypes);
if (decl != null) {
return decl;
}
return parent == null ? null : parent.getDeclaration(name, includeTypes);
}
use of com.google.javascript.jscomp.newtypes.Declaration in project closure-compiler by google.
the class NTIScope method getNamespace.
Namespace getNamespace(String name) {
checkArgument(!name.contains("."));
Declaration decl = getDeclaration(name, false);
return decl == null ? null : decl.getNamespace();
}
use of com.google.javascript.jscomp.newtypes.Declaration in project closure-compiler by google.
the class SimpleInference method inferPropAccess.
private JSType inferPropAccess(Node recv, String pname, NTIScope scope) {
if (recv.isGetProp() && recv.getLastChild().getString().equals("prototype")) {
return inferPrototypeProperty(recv.getFirstChild(), pname, scope);
}
QualifiedName propQname = new QualifiedName(pname);
JSType recvType = null;
if (recv.isQualifiedName()) {
QualifiedName recvQname = QualifiedName.fromNode(recv);
Declaration decl = scope.getDeclaration(recvQname, false);
if (decl != null) {
EnumType et = decl.getEnum();
if (et != null && et.enumLiteralHasKey(pname)) {
return et.getPropType();
}
Namespace ns = decl.getNamespace();
if (ns != null) {
return inferDeclaration(ns.getDeclaration(propQname));
}
recvType = decl.getTypeOfSimpleDecl();
}
}
if (recvType == null) {
recvType = inferExprRecur(recv, scope);
}
if (recvType == null) {
return null;
}
if (recvType.isScalar()) {
recvType = recvType.autobox();
}
FunctionType ft = recvType.getFunTypeIfSingletonObj();
if (ft != null && pname.equals("call")) {
return this.commonTypes.fromFunctionType(ft.transformByCallProperty());
} else if (ft != null && pname.equals("apply")) {
return this.commonTypes.fromFunctionType(ft.transformByApplyProperty());
}
if (recvType.mayHaveProp(propQname)) {
return recvType.getProp(propQname);
}
return null;
}
use of com.google.javascript.jscomp.newtypes.Declaration in project closure-compiler by google.
the class SimpleInference method inferInstantiatedCallee.
FunctionType inferInstantiatedCallee(Node call, FunctionType calleeType, boolean bailForUntypedArguments, NTIScope scope) {
Node callee = call.getFirstChild();
Preconditions.checkArgument(calleeType.isGeneric(), "Expected generic type for %s but found %s", callee, calleeType);
// The receiver type is useful for inference when calleeType has a @this annotation
// that includes a type variable.
JSType recvType = null;
if (callee.isGetProp() && callee.getFirstChild().isQualifiedName()) {
Node recv = callee.getFirstChild();
QualifiedName recvQname = QualifiedName.fromNode(recv);
Declaration decl = scope.getDeclaration(recvQname, false);
if (decl != null) {
recvType = decl.getTypeOfSimpleDecl();
}
}
ImmutableList.Builder<JSType> argTypes = ImmutableList.builder();
for (Node argNode = call.getSecondChild(); argNode != null; argNode = argNode.getNext()) {
JSType t = inferExprRecur(argNode, scope);
if (t == null) {
if (bailForUntypedArguments && !argNode.isFunction()) {
// Used for @const inference, where we want to be strict.
return null;
} else {
// Used when inferring a signature for unannotated callbacks passed to generic
// functions. Whatever type variable we can't infer will become unknown.
t = this.commonTypes.BOTTOM;
}
}
argTypes.add(t);
}
return calleeType.instantiateGenericsFromArgumentTypes(recvType, argTypes.build());
}
use of com.google.javascript.jscomp.newtypes.Declaration in project closure-compiler by google.
the class GlobalTypeInfoCollector method process.
@Override
public void process(Node externs, Node root) {
checkNotNull(warnings, "Cannot rerun GlobalTypeInfoCollector.process");
checkArgument(externs == null || externs.isRoot());
checkArgument(root.isRoot(), "Root must be ROOT, but is %s", root.getToken());
this.compiler.setMostRecentTypechecker(MostRecentTypechecker.NTI);
NTIScope globalScope = new NTIScope(root, null, ImmutableList.<String>of(), getCommonTypes());
globalScope.addUnknownTypeNames(this.globalTypeInfo.getUnknownTypeNames());
this.globalTypeInfo.setGlobalScope(globalScope);
this.scopes.add(globalScope);
// Processing of a scope is split into many separate phases, and it's not
// straightforward to remember which phase does what.
// (1) Find names of classes, interfaces, typedefs, enums, and namespaces
// defined in the global scope.
CollectNamedTypes rootCnt = new CollectNamedTypes(globalScope);
NodeTraversal.traverseEs6(this.compiler, externs, this.orderedExterns);
rootCnt.collectNamedTypesInExterns();
defineObjectAndFunctionIfMissing();
NodeTraversal.traverseEs6(compiler, root, rootCnt);
// (2) Determine the type represented by each typedef and each enum
globalScope.resolveTypedefs(getTypeParser());
globalScope.resolveEnums(getTypeParser());
// (3) Repeat steps 1-2 for all the other scopes (outer-to-inner)
for (int i = 1; i < this.scopes.size(); i++) {
NTIScope s = this.scopes.get(i);
CollectNamedTypes cnt = new CollectNamedTypes(s);
NodeTraversal.traverseEs6(compiler, s.getBody(), cnt);
s.resolveTypedefs(getTypeParser());
s.resolveEnums(getTypeParser());
if (NewTypeInference.measureMem) {
NewTypeInference.updatePeakMem();
}
}
// (4) The bulk of the global-scope processing happens here:
// - Create scopes for functions
// - Declare properties on types
ProcessScope rootPs = new ProcessScope(globalScope);
if (externs != null) {
NodeTraversal.traverseEs6(compiler, externs, rootPs);
}
NodeTraversal.traverseEs6(compiler, root, rootPs);
// (5) Things that must happen after the traversal of the scope
rootPs.finishProcessingScope();
// (6) Repeat steps 4-5 for all the other scopes (outer-to-inner)
for (int i = 1; i < this.scopes.size(); i++) {
NTIScope s = this.scopes.get(i);
ProcessScope ps = new ProcessScope(s);
NodeTraversal.traverseEs6(compiler, s.getBody(), ps);
ps.finishProcessingScope();
if (NewTypeInference.measureMem) {
NewTypeInference.updatePeakMem();
}
}
// (7) Adjust types of properties based on inheritance information.
// Report errors in the inheritance chain. Do Window last.
Collection<RawNominalType> windows = new ArrayList<>();
for (Map.Entry<Node, RawNominalType> entry : nominaltypesByNode.entrySet()) {
RawNominalType rawType = entry.getValue();
if (this.window != null && rawType.hasAncestorClass(this.window)) {
windows.add(rawType);
continue;
}
checkAndFreezeNominalType(rawType);
}
JSType globalThisType = null;
if (this.window != null) {
// Copy properties from window to Window.prototype, because sometimes
// people pass window around rather than using it directly.
Namespace winNs = globalScope.getNamespace(WINDOW_INSTANCE);
if (winNs != null) {
winNs.copyWindowProperties(getCommonTypes(), this.window);
}
for (RawNominalType rawType : windows) {
checkAndFreezeNominalType(rawType);
}
if (winNs != null) {
((NamespaceLit) winNs).setWindowType(this.window.getAsNominalType());
// Type the global THIS as window
globalThisType = winNs.toJSType();
}
}
if (globalThisType == null) {
// Type the global THIS as a loose object
globalThisType = getCommonTypes().getTopObject().withLoose();
}
getCommonTypes().setGlobalThis(globalThisType);
globalScope.setDeclaredType((new FunctionTypeBuilder(getCommonTypes())).addReceiverType(globalThisType).buildDeclaration());
this.globalTypeInfo.setRawNominalTypes(nominaltypesByNode.values());
nominaltypesByNode = null;
propertyDefs = null;
for (NTIScope s : this.scopes) {
s.freezeScope();
}
this.simpleInference.setScopesAreFrozen();
// Traverse the externs and annotate them with types.
// Only works for the top level, not inside function bodies.
NodeTraversal.traverseEs6(this.compiler, externs, new NodeTraversal.AbstractShallowCallback() {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.isQualifiedName()) {
Declaration d = getGlobalScope().getDeclaration(QualifiedName.fromNode(n), false);
JSType type = simpleInferDeclaration(d);
if (type == null) {
type = simpleInferExpr(n, getGlobalScope());
}
// Type-based passes expect the externs to be annotated, so use ? when type is null.
n.setTypeI(type != null ? type : getCommonTypes().UNKNOWN);
}
}
});
Map<Node, String> unknownTypes = getTypeParser().getUnknownTypesMap();
for (Map.Entry<Node, String> unknownTypeEntry : unknownTypes.entrySet()) {
this.warnings.add(JSError.make(unknownTypeEntry.getKey(), UNRECOGNIZED_TYPE_NAME, unknownTypeEntry.getValue()));
}
// package, so we collect its warnings here.
for (JSError warning : getTypeParser().getWarnings()) {
this.warnings.add(warning);
}
this.warnings = null;
this.funNameGen = null;
reorderScopesForNTI();
this.compiler.setExternProperties(ImmutableSet.copyOf(getExternPropertyNames()));
}
Aggregations