当前位置: 首页>>代码示例>>Java>>正文


Java Ref.getTwin方法代码示例

本文整理汇总了Java中com.google.javascript.jscomp.GlobalNamespace.Ref.getTwin方法的典型用法代码示例。如果您正苦于以下问题:Java Ref.getTwin方法的具体用法?Java Ref.getTwin怎么用?Java Ref.getTwin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.javascript.jscomp.GlobalNamespace.Ref的用法示例。


在下文中一共展示了Ref.getTwin方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: CollectDefines

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) {
  this.compiler = compiler;
  this.allDefines = Maps.newHashMap();

  assignableDefines = Maps.newHashMap();
  assignAllowed = new ArrayDeque<Integer>();
  assignAllowed.push(1);

  // Create a map of references to defines keyed by node for easy lookup
  allRefInfo = Maps.newHashMap();
  for (Name name : listOfDefines) {
    if (name.declaration != null) {
      allRefInfo.put(name.declaration.node,
                     new RefInfo(name.declaration, name));
    }
    if (name.refs != null) {
      for (Ref ref : name.refs) {
        // If there's a TWIN def, only put one of the twins in.
        if (ref.getTwin() == null || !ref.getTwin().isSet()) {
          allRefInfo.put(ref.node, new RefInfo(ref, name));
        }
      }
    }
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:26,代码来源:ProcessDefines.java

示例2: flattenReferencesTo

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all references to a collapsible property of a global name except
 * its initial definition.
 *
 * @param n A global property name (e.g. "a.b" or "a.b.c.d")
 * @param alias The flattened name (e.g. "a$b" or "a$b$c$d")
 */
private void flattenReferencesTo(Name n, String alias) {
  if (n.refs != null) {
    String originalName = n.fullName();
    for (Ref r : n.refs) {
      Node rParent = r.node.getParent();

      // There are two cases when we shouldn't flatten a reference:
      // 1) Object literal keys, because duplicate keys show up as refs.
      // 2) References inside a complex assign. (a = x.y = 0). These are
      //    called TWIN references, because they show up twice in the
      //    reference list. Only collapse the set, not the alias.
      if (!NodeUtil.isObjectLitKey(r.node, rParent) &&
          (r.getTwin() == null || r.isSet())) {
        flattenNameRef(alias, r.node, rParent, originalName);
      }
    }
  }

  // Flatten all occurrences of a name as a prefix of its subnames. For
  // example, if {@code n} corresponds to the name "a.b", then "a.b" will be
  // replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, 1);
    }
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:35,代码来源:CollapseProperties.java

示例3: flattenPrefixes

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all occurrences of a name as a prefix of subnames beginning
 * with a particular subname.
 *
 * @param n A global property name (e.g. "a.b.c.d")
 * @param alias A flattened prefix name (e.g. "a$b")
 * @param depth The difference in depth between the property name and
 *    the prefix name (e.g. 2)
 */
private void flattenPrefixes(String alias, Name n, int depth) {
  // Only flatten the prefix of a name declaration if the name being
  // initialized is fully qualified (i.e. not an object literal key).
  String originalName = n.fullName();
  if (n.declaration != null && n.declaration.node != null &&
      n.declaration.node.getType() == Token.GETPROP) {
    flattenNameRefAtDepth(alias, n.declaration.node, depth, originalName);
  }

  if (n.refs != null) {
    for (Ref r : n.refs) {

      // References inside a complex assign (a = x.y = 0)
      // have twins. We should only flatten one of the twins.
      if (r.getTwin() == null || r.isSet()) {
        flattenNameRefAtDepth(alias, r.node, depth, originalName);
      }
    }
  }

  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, depth + 1);
    }
  }
}
 
开发者ID:andyjko,项目名称:feedlack,代码行数:36,代码来源:CollapseProperties.java

示例4: CollectDefines

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) {
  this.compiler = compiler;
  this.allDefines = Maps.newHashMap();

  assignableDefines = Maps.newHashMap();
  assignAllowed = new ArrayDeque<Integer>();
  assignAllowed.push(1);

  // Create a map of references to defines keyed by node for easy lookup
  allRefInfo = Maps.newHashMap();
  for (Name name : listOfDefines) {
    Ref decl = name.getDeclaration();
    if (decl != null) {
      allRefInfo.put(decl.node,
                     new RefInfo(decl, name));
    }
    for (Ref ref : name.getRefs()) {
      if (ref == decl) {
        // Declarations were handled above.
        continue;
      }

      // If there's a TWIN def, only put one of the twins in.
      if (ref.getTwin() == null || !ref.getTwin().isSet()) {
        allRefInfo.put(ref.node, new RefInfo(ref, name));
      }
    }
  }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:30,代码来源:ProcessDefines.java

示例5: flattenReferencesTo

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all references to a collapsible property of a global name except
 * its initial definition.
 *
 * @param n A global property name (e.g. "a.b" or "a.b.c.d")
 * @param alias The flattened name (e.g. "a$b" or "a$b$c$d")
 */
private void flattenReferencesTo(Name n, String alias) {
  String originalName = n.getFullName();
  for (Ref r : n.getRefs()) {
    if (r == n.getDeclaration()) {
      // Declarations are handled separately.
      continue;
    }

    Node rParent = r.node.getParent();

    // There are two cases when we shouldn't flatten a reference:
    // 1) Object literal keys, because duplicate keys show up as refs.
    // 2) References inside a complex assign. (a = x.y = 0). These are
    //    called TWIN references, because they show up twice in the
    //    reference list. Only collapse the set, not the alias.
    if (!NodeUtil.isObjectLitKey(r.node, rParent) &&
        (r.getTwin() == null || r.isSet())) {
      flattenNameRef(alias, r.node, rParent, originalName);
    }
  }

  // Flatten all occurrences of a name as a prefix of its subnames. For
  // example, if {@code n} corresponds to the name "a.b", then "a.b" will be
  // replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, 1);
    }
  }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:38,代码来源:CollapseProperties.java

示例6: flattenPrefixes

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all occurrences of a name as a prefix of subnames beginning
 * with a particular subname.
 *
 * @param n A global property name (e.g. "a.b.c.d")
 * @param alias A flattened prefix name (e.g. "a$b")
 * @param depth The difference in depth between the property name and
 *    the prefix name (e.g. 2)
 */
private void flattenPrefixes(String alias, Name n, int depth) {
  // Only flatten the prefix of a name declaration if the name being
  // initialized is fully qualified (i.e. not an object literal key).
  String originalName = n.getFullName();
  Ref decl = n.getDeclaration();
  if (decl != null && decl.node != null &&
      decl.node.isGetProp()) {
    flattenNameRefAtDepth(alias, decl.node, depth, originalName);
  }

  for (Ref r : n.getRefs()) {
    if (r == decl) {
      // Declarations are handled separately.
      continue;
    }

    // References inside a complex assign (a = x.y = 0)
    // have twins. We should only flatten one of the twins.
    if (r.getTwin() == null || r.isSet()) {
      flattenNameRefAtDepth(alias, r.node, depth, originalName);
    }
  }

  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, depth + 1);
    }
  }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:39,代码来源:CollapseProperties.java

示例7: updateObjLitOrFunctionDeclaration

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Updates the first initialization (a.k.a "declaration") of a global name.
 * This involves flattening the global name (if it's not just a global
 * variable name already), collapsing object literal keys into global
 * variables, declaring stub global variables for properties added later
 * in a local scope.
 *
 * It may seem odd that this function also takes care of declaring stubs
 * for direct children. The ultimate goal of this function is to eliminate
 * the global name entirely (when possible), so that "middlemen" namespaces
 * disappear, and to do that we need to make sure that all the direct children
 * will be collapsed as well.
 *
 * @param n An object representing a global name (e.g. "a", "a.b.c")
 * @param alias The flattened name for {@code n} (e.g. "a", "a$b$c")
 * @param canCollapseChildNames Whether it's possible to collapse children of
 *     this name. (This is mostly passed for convenience; it's equivalent to
 *     n.canCollapseChildNames()).
 */
private void updateObjLitOrFunctionDeclaration(
    Name n, String alias, boolean canCollapseChildNames) {
  Ref decl = n.getDeclaration();
  if (decl == null) {
    // Some names do not have declarations, because they
    // are only defined in local scopes.
    return;
  }

  if (decl.getTwin() != null) {
    // Twin declarations will get handled when normal references
    // are handled.
    return;
  }

  switch (decl.node.getParent().getType()) {
    case Token.ASSIGN:
      updateObjLitOrFunctionDeclarationAtAssignNode(
          n, alias, canCollapseChildNames);
      break;
    case Token.VAR:
      updateObjLitOrFunctionDeclarationAtVarNode(n, canCollapseChildNames);
      break;
    case Token.FUNCTION:
      updateFunctionDeclarationAtFunctionNode(n, canCollapseChildNames);
      break;
  }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:48,代码来源:CollapseProperties.java

示例8: CollectDefines

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) {
  this.compiler = compiler;
  this.allDefines = new HashMap<>();

  assignableDefines = new HashMap<>();
  assignAllowed = new ArrayDeque<>();
  assignAllowed.push(1);

  // Create a map of references to defines keyed by node for easy lookup
  allRefInfo = new HashMap<>();
  for (Name name : listOfDefines) {
    Ref decl = name.getDeclaration();
    if (decl != null) {
      allRefInfo.put(decl.node,
                     new RefInfo(decl, name));
    }
    for (Ref ref : name.getRefs()) {
      if (ref == decl) {
        // Declarations were handled above.
        continue;
      }

      // If there's a TWIN def, only put one of the twins in.
      if (ref.getTwin() == null || !ref.getTwin().isSet()) {
        allRefInfo.put(ref.node, new RefInfo(ref, name));
      }
    }
  }
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:30,代码来源:ProcessDefines.java

示例9: flattenReferencesTo

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all references to a collapsible property of a global name except
 * its initial definition.
 *
 * @param n A global property name (e.g. "a.b" or "a.b.c.d")
 * @param alias The flattened name (e.g. "a$b" or "a$b$c$d")
 */
private void flattenReferencesTo(Name n, String alias) {
  String originalName = n.getFullName();
  for (Ref r : n.getRefs()) {
    if (r == n.getDeclaration()) {
      // Declarations are handled separately.
      continue;
    }
    Node rParent = r.node.getParent();
    // There are two cases when we shouldn't flatten a reference:
    // 1) Object literal keys, because duplicate keys show up as refs.
    // 2) References inside a complex assign. (a = x.y = 0). These are
    //    called TWIN references, because they show up twice in the
    //    reference list. Only collapse the set, not the alias.
    if (!NodeUtil.isObjectLitKey(r.node) && (r.getTwin() == null || r.isSet())) {
      flattenNameRef(alias, r.node, rParent, originalName);
    }
  }

  // Flatten all occurrences of a name as a prefix of its subnames. For
  // example, if {@code n} corresponds to the name "a.b", then "a.b" will be
  // replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, 1);
    }
  }
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:35,代码来源:CollapseProperties.java

示例10: flattenPrefixes

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all occurrences of a name as a prefix of subnames beginning
 * with a particular subname.
 *
 * @param n A global property name (e.g. "a.b.c.d")
 * @param alias A flattened prefix name (e.g. "a$b")
 * @param depth The difference in depth between the property name and
 *    the prefix name (e.g. 2)
 */
private void flattenPrefixes(String alias, Name n, int depth) {
  // Only flatten the prefix of a name declaration if the name being
  // initialized is fully qualified (i.e. not an object literal key).
  String originalName = n.getFullName();
  Ref decl = n.getDeclaration();
  if (decl != null && decl.node != null && decl.node.isGetProp()) {
    flattenNameRefAtDepth(alias, decl.node, depth, originalName);
  }

  for (Ref r : n.getRefs()) {
    if (r == decl) {
      // Declarations are handled separately.
      continue;
    }

    // References inside a complex assign (a = x.y = 0)
    // have twins. We should only flatten one of the twins.
    if (r.getTwin() == null || r.isSet()) {
      flattenNameRefAtDepth(alias, r.node, depth, originalName);
    }
  }

  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, depth + 1);
    }
  }
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:38,代码来源:CollapseProperties.java

示例11: CollectDefines

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) {
  this.compiler = compiler;
  this.allDefines = Maps.newHashMap();

  assignableDefines = Maps.newHashMap();
  assignAllowed = new ArrayDeque<>();
  assignAllowed.push(1);

  // Create a map of references to defines keyed by node for easy lookup
  allRefInfo = Maps.newHashMap();
  for (Name name : listOfDefines) {
    Ref decl = name.getDeclaration();
    if (decl != null) {
      allRefInfo.put(decl.node,
                     new RefInfo(decl, name));
    }
    for (Ref ref : name.getRefs()) {
      if (ref == decl) {
        // Declarations were handled above.
        continue;
      }

      // If there's a TWIN def, only put one of the twins in.
      if (ref.getTwin() == null || !ref.getTwin().isSet()) {
        allRefInfo.put(ref.node, new RefInfo(ref, name));
      }
    }
  }
}
 
开发者ID:nicks,项目名称:closure-compiler-old,代码行数:30,代码来源:ProcessDefines.java

示例12: flattenReferencesTo

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Flattens all references to a collapsible property of a global name except
 * its initial definition.
 *
 * @param n A global property name (e.g. "a.b" or "a.b.c.d")
 * @param alias The flattened name (e.g. "a$b" or "a$b$c$d")
 */
private void flattenReferencesTo(Name n, String alias) {
  String originalName = n.getFullName();
  for (Ref r : n.getRefs()) {
    if (r == n.getDeclaration()) {
      // Declarations are handled separately.
      continue;
    }

    Node rParent = r.node.getParent();

    // There are two cases when we shouldn't flatten a reference:
    // 1) Object literal keys, because duplicate keys show up as refs.
    // 2) References inside a complex assign. (a = x.y = 0). These are
    //    called TWIN references, because they show up twice in the
    //    reference list. Only collapse the set, not the alias.
    if (!NodeUtil.isObjectLitKey(r.node) &&
        (r.getTwin() == null || r.isSet())) {
      flattenNameRef(alias, r.node, rParent, originalName);
    }
  }

  // Flatten all occurrences of a name as a prefix of its subnames. For
  // example, if {@code n} corresponds to the name "a.b", then "a.b" will be
  // replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
  if (n.props != null) {
    for (Name p : n.props) {
      flattenPrefixes(alias, p, 1);
    }
  }
}
 
开发者ID:nicks,项目名称:closure-compiler-old,代码行数:38,代码来源:CollapseProperties.java

示例13: inlineAliases

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * For each qualified name N in the global scope, we check if: (a) No ancestor of N is ever
 * aliased or assigned an unknown value type. (If N = "a.b.c", "a" and "a.b" are never aliased).
 * (b) N has exactly one write, and it lives in the global scope. (c) N is aliased in a local
 * scope. (d) N is aliased in global scope
 *
 * <p>If (a) is true, then GlobalNamespace must know all the writes to N. If (a) and (b) are true,
 * then N cannot change during the execution of a local scope. If (a) and (b) and (c) are true,
 * then the alias can be inlined if the alias obeys the usual rules for how we decide whether a
 * variable is inlineable. If (a) and (b) and (d) are true, then inline the alias if possible (if
 * it is assigned exactly once unconditionally).
 *
 * <p>For (a), (b), and (c) are true and the alias is of a constructor, we may also partially
 * inline the alias - i.e. replace some references with the constructor but not all - since
 * constructor properties are always collapsed, so we want to be more aggressive about removing
 * aliases.
 *
 * @see InlineVariables
 */
private void inlineAliases(GlobalNamespace namespace) {
  // Invariant: All the names in the worklist meet condition (a).
  Deque<Name> workList = new ArrayDeque<>(namespace.getNameForest());

  while (!workList.isEmpty()) {
    Name name = workList.pop();

    // Don't attempt to inline a getter or setter property as a variable.
    if (name.type == Name.Type.GET || name.type == Name.Type.SET) {
      continue;
    }

    if (!name.inExterns && name.globalSets == 1 && name.localSets == 0 && name.aliasingGets > 0) {
      // {@code name} meets condition (b). Find all of its local aliases
      // and try to inline them.
      List<Ref> refs = new ArrayList<>(name.getRefs());
      for (Ref ref : refs) {
        Scope hoistScope = ref.scope.getClosestHoistScope();
        if (ref.type == Type.ALIASING_GET && !mayBeGlobalAlias(ref) && ref.getTwin() == null) {
          // {@code name} meets condition (c). Try to inline it.
          // TODO(johnlenz): consider picking up new aliases at the end
          // of the pass instead of immediately like we do for global
          // inlines.
          if (inlineAliasIfPossible(name, ref, namespace)) {
            name.removeRef(ref);
          }
        } else if (ref.type == Type.ALIASING_GET
            && hoistScope.isGlobal()
            && ref.getTwin() == null) { // ignore aliases in chained assignments
          if (inlineGlobalAliasIfPossible(name, ref, namespace)) {
            name.removeRef(ref);
          }
        }
      }
    }

    if (!name.inExterns && name.type == Name.Type.CLASS) {
      List<Name> subclasses = name.subclasses;
      if (subclasses != null && name.props != null) {
        for (Name subclass : subclasses) {
          for (Name prop : name.props) {
            rewriteAllSubclassInheritedAccesses(name, subclass, prop, namespace);
          }
        }
      }
    }

    // Check if {@code name} has any aliases left after the
    // local-alias-inlining above.
    if ((name.type == Name.Type.OBJECTLIT
            || name.type == Name.Type.FUNCTION
            || name.type == Name.Type.CLASS)
        && name.aliasingGets == 0
        && name.props != null) {
      // All of {@code name}'s children meet condition (a), so they can be
      // added to the worklist.
      workList.addAll(name.props);
    }
  }
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:80,代码来源:AggressiveInlineAliases.java

示例14: updateGlobalNameDeclarationAtAssignNode

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * Updates the first initialization (a.k.a "declaration") of a global name
 * that occurs at an ASSIGN node. See comment for
 * {@link #updateGlobalNameDeclaration}.
 *
 * @param n An object representing a global name (e.g. "a", "a.b.c")
 * @param alias The flattened name for {@code n} (e.g. "a", "a$b$c")
 */
private void updateGlobalNameDeclarationAtAssignNode(
    Name n, String alias, boolean canCollapseChildNames) {
  // NOTE: It's important that we don't add additional nodes
  // (e.g. a var node before the exprstmt) because the exprstmt might be
  // the child of an if statement that's not inside a block).

  // All qualified names - even for variables that are initially declared as LETS and CONSTS -
  // are being declared as VAR statements, but this is not incorrect because
  // we are only collapsing for global names.
  Ref ref = n.getDeclaration();
  Node rvalue = ref.node.getNext();
  if (ref.getTwin() != null) {
    updateTwinnedDeclaration(alias, ref.name, ref);
    return;
  }
  Node varNode = new Node(Token.VAR);
  Node varParent = ref.node.getAncestor(3);
  Node grandparent = ref.node.getAncestor(2);
  boolean isObjLit = rvalue.isObjectLit();
  boolean insertedVarNode = false;

  if (isObjLit && canEliminate(n)) {
    // Eliminate the object literal altogether.
    varParent.replaceChild(grandparent, varNode);
    ref.node = null;
    insertedVarNode = true;
    compiler.reportChangeToEnclosingScope(varNode);
  } else if (!n.isSimpleName()) {
    // Create a VAR node to declare the name.
    if (rvalue.isFunction()) {
      checkForHosedThisReferences(rvalue, n.docInfo, n);
    }

    compiler.reportChangeToEnclosingScope(rvalue);
    ref.node.getParent().removeChild(rvalue);

    Node nameNode = NodeUtil.newName(compiler,
        alias, ref.node.getAncestor(2), n.getFullName());

    JSDocInfo info = NodeUtil.getBestJSDocInfo(ref.node.getParent());
    if (ref.node.getLastChild().getBooleanProp(Node.IS_CONSTANT_NAME)
        || (info != null && info.isConstant())) {
      nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true);
    }

    if (info != null) {
      varNode.setJSDocInfo(info);
    }
    varNode.addChildToBack(nameNode);
    nameNode.addChildToFront(rvalue);
    varParent.replaceChild(grandparent, varNode);

    // Update the node ancestry stored in the reference.
    ref.node = nameNode;
    insertedVarNode = true;
    compiler.reportChangeToEnclosingScope(varNode);
  }

  if (canCollapseChildNames) {
    if (isObjLit) {
      declareVariablesForObjLitValues(
          n, alias, rvalue, varNode, varNode.getPrevious(), varParent);
    }

    addStubsForUndeclaredProperties(n, alias, varParent, varNode);
  }

  if (insertedVarNode) {
    if (!varNode.hasChildren()) {
      varParent.removeChild(varNode);
    }
  }
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:82,代码来源:CollapseProperties.java

示例15: inlineAliases

import com.google.javascript.jscomp.GlobalNamespace.Ref; //导入方法依赖的package包/类
/**
 * For each qualified name N in the global scope, we check if:
 * (a) No ancestor of N is ever aliased or assigned an unknown value type.
 *     (If N = "a.b.c", "a" and "a.b" are never aliased).
 * (b) N has exactly one write, and it lives in the global scope.
 * (c) N is aliased in a local scope.
 * (d) N is aliased in global scope
 *
 * If (a) is true, then GlobalNamespace must know all the writes to N.
 * If (a) and (b) are true, then N cannot change during the execution of
 *    a local scope.
 * If (a) and (b) and (c) are true, then the alias can be inlined if the
 *    alias obeys the usual rules for how we decide whether a variable is
 *    inlineable.
 * If (a) and (b) and (d) are true, then inline the alias if possible (if
 * it is assigned exactly once unconditionally).
 * @see InlineVariables
 */
private void inlineAliases(GlobalNamespace namespace) {
  // Invariant: All the names in the worklist meet condition (a).
  Deque<Name> workList = new ArrayDeque<>(namespace.getNameForest());

  while (!workList.isEmpty()) {
    Name name = workList.pop();

    // Don't attempt to inline a getter or setter property as a variable.
    if (name.type == Name.Type.GET || name.type == Name.Type.SET) {
      continue;
    }

    if (!name.inExterns && name.globalSets == 1 && name.localSets == 0 &&
        name.aliasingGets > 0) {
      // {@code name} meets condition (b). Find all of its local aliases
      // and try to inline them.
      List<Ref> refs = Lists.newArrayList(name.getRefs());
      for (Ref ref : refs) {
        if (ref.type == Type.ALIASING_GET && ref.scope.isLocal()) {
          // {@code name} meets condition (c). Try to inline it.
          // TODO(johnlenz): consider picking up new aliases at the end
          // of the pass instead of immediately like we do for global
          // inlines.
          if (inlineAliasIfPossible(ref, namespace)) {
            name.removeRef(ref);
          }
        } else if (ref.type == Type.ALIASING_GET
            && ref.scope.isGlobal()
            && ref.getTwin() == null) {  // ignore aliases in chained assignments
          if (inlineGlobalAliasIfPossible(ref, namespace)) {
            name.removeRef(ref);
          }
        }
      }
    }

    // Check if {@code name} has any aliases left after the
    // local-alias-inlining above.
    if ((name.type == Name.Type.OBJECTLIT ||
         name.type == Name.Type.FUNCTION) &&
        name.aliasingGets == 0 && name.props != null) {
      // All of {@code name}'s children meet condition (a), so they can be
      // added to the worklist.
      workList.addAll(name.props);
    }
  }
}
 
开发者ID:nicks,项目名称:closure-compiler-old,代码行数:66,代码来源:CollapseProperties.java


注:本文中的com.google.javascript.jscomp.GlobalNamespace.Ref.getTwin方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。