Exemple #1
0
  private static SMAP createSMAPWithDefaultMapping(
      @NotNull JetExpression declaration, @NotNull List<FileMapping> mappings) {
    PsiFile containingFile = declaration.getContainingFile();
    Integer lineNumbers = CodegenUtil.getLineNumberForElement(containingFile, true);
    assert lineNumbers != null : "Couldn't extract line count in " + containingFile;

    return new SMAP(mappings);
  }
Exemple #2
0
  public static boolean isSelectorInQualified(@NotNull JetSimpleNameExpression nameExpression) {
    PsiElement nameExpressionParent = nameExpression.getParent();

    if (nameExpressionParent instanceof JetUserType) {
      assert ((JetUserType) nameExpressionParent).getReferenceExpression() == nameExpression;
      return ((JetUserType) nameExpressionParent).getQualifier() != null;
    }

    JetExpression selector = nameExpression;
    if (nameExpressionParent instanceof JetCallExpression
        && ((JetCallExpression) nameExpressionParent).getCalleeExpression() == nameExpression) {
      selector = (JetCallExpression) nameExpressionParent;
    }

    PsiElement selectorParent = selector.getParent();
    return selectorParent instanceof JetQualifiedExpression
        && (((JetQualifiedExpression) selectorParent).getSelectorExpression() == selector);
  }
 private static JetScope getExpressionResolutionScope(
     @NotNull ResolveSession resolveSession, @NotNull JetExpression expression) {
   ScopeProvider provider = resolveSession.getInjector().getScopeProvider();
   JetDeclaration parentDeclaration =
       PsiTreeUtil.getParentOfType(expression, JetDeclaration.class);
   if (parentDeclaration == null) {
     return provider.getFileScopeWithAllImported((JetFile) expression.getContainingFile());
   }
   return provider.getResolutionScopeForDeclaration(parentDeclaration);
 }
Exemple #4
0
  public void rememberClosure(JetExpression expression, Type type) {
    JetExpression lambda = JetPsiUtil.deparenthesize(expression);
    assert isInlinableParameterExpression(lambda)
        : "Couldn't find inline expression in " + expression.getText();

    LambdaInfo info = new LambdaInfo(lambda, typeMapper);

    ParameterInfo closureInfo = invocationParamBuilder.addNextParameter(type, true, null);
    closureInfo.setLambda(info);
    expressionMap.put(closureInfo.getIndex(), info);
  }
  private static String getDefaultExpressionString(@Nullable PsiElement parameterDeclaration) {
    if (parameterDeclaration instanceof JetParameter) {
      JetExpression defaultValue = ((JetParameter) parameterDeclaration).getDefaultValue();
      if (defaultValue != null) {
        String defaultExpression = defaultValue.getText();
        if (defaultExpression.length() <= 32) {
          return defaultExpression;
        }

        if (defaultValue instanceof JetConstantExpression
            || defaultValue instanceof JetStringTemplateExpression) {
          if (defaultExpression.startsWith("\"")) {
            return "\"...\"";
          } else if (defaultExpression.startsWith("\'")) {
            return "\'...\'";
          }
        }
      }
    }
    return "...";
  }
  public static @NotNull BindingContext resolveToExpression(
      @NotNull final ResolveSession resolveSession, @NotNull JetExpression expression) {
    final DelegatingBindingTrace trace =
        new DelegatingBindingTrace(
            resolveSession.getBindingContext(), "trace to resolve expression", expression);
    JetFile file = (JetFile) expression.getContainingFile();

    @SuppressWarnings("unchecked")
    PsiElement topmostCandidateForAdditionalResolve =
        JetPsiUtil.getTopmostParentOfTypes(
            expression,
            JetNamedFunction.class,
            JetClassInitializer.class,
            JetProperty.class,
            JetDelegationSpecifierList.class);

    if (topmostCandidateForAdditionalResolve != null) {
      if (topmostCandidateForAdditionalResolve instanceof JetNamedFunction) {
        functionAdditionalResolve(
            resolveSession, (JetNamedFunction) topmostCandidateForAdditionalResolve, trace, file);
      } else if (topmostCandidateForAdditionalResolve instanceof JetClassInitializer) {
        initializerAdditionalResolve(
            resolveSession,
            (JetClassInitializer) topmostCandidateForAdditionalResolve,
            trace,
            file);
      } else if (topmostCandidateForAdditionalResolve instanceof JetProperty) {
        propertyAdditionalResolve(
            resolveSession, (JetProperty) topmostCandidateForAdditionalResolve, trace, file);
      } else if (topmostCandidateForAdditionalResolve instanceof JetDelegationSpecifierList) {
        delegationSpecifierAdditionalResolve(
            resolveSession,
            (JetDelegationSpecifierList) topmostCandidateForAdditionalResolve,
            trace,
            file);
      } else {
        assert false : "Invalid type of the topmost parent";
      }

      return trace.getBindingContext();
    }

    // Setup resolution scope explicitly
    if (trace.getBindingContext().get(BindingContext.RESOLUTION_SCOPE, expression) == null) {
      JetScope scope = getExpressionMemberScope(resolveSession, expression);
      if (scope != null) {
        trace.record(BindingContext.RESOLUTION_SCOPE, expression, scope);
      }
    }

    return trace.getBindingContext();
  }
Exemple #7
0
  @Nullable
  private static Name getName(@Nullable JetExpression expression) {
    if (expression == null) {
      return null;
    }

    if (expression instanceof JetSimpleNameExpression) {
      return ((JetSimpleNameExpression) expression).getReferencedNameAsName();
    } else {
      throw new IllegalArgumentException(
          "Can't construct name for: " + expression.getClass().toString());
    }
  }
Exemple #8
0
  @Nullable
  private static FqName getFQName(@Nullable JetExpression expression) {
    if (expression == null) {
      return null;
    }

    if (expression instanceof JetDotQualifiedExpression) {
      JetDotQualifiedExpression dotQualifiedExpression = (JetDotQualifiedExpression) expression;
      FqName parentFqn = getFQName(dotQualifiedExpression.getReceiverExpression());
      Name child = getName(dotQualifiedExpression.getSelectorExpression());

      return parentFqn != null && child != null ? parentFqn.child(child) : null;
    } else if (expression instanceof JetSimpleNameExpression) {
      JetSimpleNameExpression simpleNameExpression = (JetSimpleNameExpression) expression;
      return FqName.topLevel(simpleNameExpression.getReferencedNameAsName());
    } else {
      throw new IllegalArgumentException(
          "Can't construct fqn for: " + expression.getClass().toString());
    }
  }
Exemple #9
0
  public static boolean areParenthesesNecessary(
      @NotNull JetExpression innerExpression,
      @NotNull JetExpression currentInner,
      @NotNull JetExpression parentExpression) {
    if (parentExpression instanceof JetParenthesizedExpression
        || innerExpression instanceof JetParenthesizedExpression) {
      return false;
    }

    if (parentExpression instanceof JetWhenExpression
        || innerExpression instanceof JetWhenExpression) {
      return false;
    }

    if (innerExpression instanceof JetIfExpression) {
      PsiElement current = parentExpression;

      while (!(current instanceof JetBlockExpression
          || current instanceof JetDeclaration
          || current instanceof JetStatementExpression)) {
        if (current.getTextRange().getEndOffset() != currentInner.getTextRange().getEndOffset()) {
          return current.getText().charAt(current.getTextLength() - 1)
              != ')'; // if current expression is "guarded" by parenthesis, no extra parenthesis is
                      // necessary
        }

        current = current.getParent();
      }
    }

    IElementType innerOperation = getOperation(innerExpression);
    IElementType parentOperation = getOperation(parentExpression);

    // 'return (@label{...})' case
    if (parentExpression instanceof JetReturnExpression
        && innerOperation == JetTokens.LABEL_IDENTIFIER) {
      return true;
    }

    // '(x: Int) < y' case
    if (innerExpression instanceof JetBinaryExpressionWithTypeRHS
        && parentOperation == JetTokens.LT) {
      return true;
    }

    int innerPriority = getPriority(innerExpression);
    int parentPriority = getPriority(parentExpression);

    if (innerPriority == parentPriority) {
      if (parentExpression instanceof JetBinaryExpression) {
        if (innerOperation == JetTokens.ANDAND || innerOperation == JetTokens.OROR) {
          return false;
        }
        return ((JetBinaryExpression) parentExpression).getRight() == currentInner;
      }

      // '-(-x)' case
      if (parentExpression instanceof JetPrefixExpression
          && innerExpression instanceof JetPrefixExpression) {
        return innerOperation == parentOperation
            && (innerOperation == JetTokens.PLUS || innerOperation == JetTokens.MINUS);
      }
      return false;
    }

    return innerPriority < parentPriority;
  }
Exemple #10
0
 public static boolean isNullConstant(@NotNull JetExpression expression) {
   JetExpression deparenthesized = deparenthesize(expression);
   return deparenthesized instanceof JetConstantExpression
       && deparenthesized.getNode().getElementType() == JetNodeTypes.NULL;
 }
Exemple #11
0
 public static boolean isLHSOfDot(@NotNull JetExpression expression) {
   PsiElement parent = expression.getParent();
   if (!(parent instanceof JetQualifiedExpression)) return false;
   JetQualifiedExpression qualifiedParent = (JetQualifiedExpression) parent;
   return qualifiedParent.getReceiverExpression() == expression || isLHSOfDot(qualifiedParent);
 }
  public static JetScope getExpressionMemberScope(
      @NotNull ResolveSession resolveSession, @NotNull JetExpression expression) {
    DelegatingBindingTrace trace =
        new DelegatingBindingTrace(
            resolveSession.getBindingContext(),
            "trace to resolve a member scope of expression",
            expression);

    if (expression instanceof JetReferenceExpression) {
      QualifiedExpressionResolver qualifiedExpressionResolver =
          resolveSession.getInjector().getQualifiedExpressionResolver();

      // In some type declaration
      if (expression.getParent() instanceof JetUserType) {
        JetUserType qualifier = ((JetUserType) expression.getParent()).getQualifier();
        if (qualifier != null) {
          Collection<? extends DeclarationDescriptor> descriptors =
              qualifiedExpressionResolver.lookupDescriptorsForUserType(
                  qualifier, getExpressionResolutionScope(resolveSession, expression), trace);

          for (DeclarationDescriptor descriptor : descriptors) {
            if (descriptor instanceof LazyPackageDescriptor) {
              return ((LazyPackageDescriptor) descriptor).getMemberScope();
            }
          }
        }
      }

      // Inside import
      if (PsiTreeUtil.getParentOfType(expression, JetImportDirective.class, false) != null) {
        NamespaceDescriptor rootPackage = resolveSession.getPackageDescriptorByFqName(FqName.ROOT);
        assert rootPackage != null;

        if (expression.getParent() instanceof JetDotQualifiedExpression) {
          JetExpression element =
              ((JetDotQualifiedExpression) expression.getParent()).getReceiverExpression();
          String name = ((JetFile) expression.getContainingFile()).getPackageName();

          NamespaceDescriptor filePackage =
              name != null
                  ? resolveSession.getPackageDescriptorByFqName(new FqName(name))
                  : rootPackage;
          assert filePackage != null : "File package should be already resolved and be found";

          JetScope scope = filePackage.getMemberScope();
          Collection<? extends DeclarationDescriptor> descriptors;

          if (element instanceof JetDotQualifiedExpression) {
            descriptors =
                qualifiedExpressionResolver.lookupDescriptorsForQualifiedExpression(
                    (JetDotQualifiedExpression) element,
                    rootPackage.getMemberScope(),
                    scope,
                    trace,
                    false,
                    false);
          } else {
            descriptors =
                qualifiedExpressionResolver.lookupDescriptorsForSimpleNameReference(
                    (JetSimpleNameExpression) element,
                    rootPackage.getMemberScope(),
                    scope,
                    trace,
                    false,
                    false,
                    false);
          }

          for (DeclarationDescriptor descriptor : descriptors) {
            if (descriptor instanceof NamespaceDescriptor) {
              return ((NamespaceDescriptor) descriptor).getMemberScope();
            }
          }
        } else {
          return rootPackage.getMemberScope();
        }
      }

      // Inside package declaration
      JetNamespaceHeader namespaceHeader =
          PsiTreeUtil.getParentOfType(expression, JetNamespaceHeader.class, false);
      if (namespaceHeader != null) {
        NamespaceDescriptor packageDescriptor =
            resolveSession.getPackageDescriptorByFqName(
                namespaceHeader.getParentFqName((JetReferenceExpression) expression));
        if (packageDescriptor != null) {
          return packageDescriptor.getMemberScope();
        }
      }
    }

    return null;
  }