@Nullable
  private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr) {
    PsiType macroType = GroovyInlineTransformationUtilKt.getTypeFromInlineTransformation(refExpr);
    if (macroType != null) return macroType;
    final Collection<? extends GroovyResolveResult> results = refExpr.lrResolve(true);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();

    PsiType typeFromCalculators = GrTypeCalculator.getTypeFromCalculators(refExpr);
    if (typeFromCalculators != null) return typeFromCalculators;

    if (ResolveUtil.isClassReference(refExpr)) {
      GrExpression qualifier = refExpr.getQualifier();
      LOG.assertTrue(qualifier != null);
      return qualifier.getType();
    }

    final PsiType nominal = refExpr.getNominalType(true);
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
      return nominal == null ? getDefaultType(refExpr, result) : nominal;
    }

    if (nominal == null) {
      if (inferred.equals(PsiTypes.nullType()) && CompileStaticUtil.isCompileStatic(refExpr)) {
        return TypesUtil.getJavaLangObject(refExpr);
      }
      else {
        return inferred;
      }
    }
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
      if (resolved instanceof GrVariable) {
        if (((GrVariable)resolved).getTypeElementGroovy() != null) {
          return nominal;
        }
      }
      else if (resolved instanceof PsiVariable) {
        return nominal;
      }
    }
    return inferred;
  }
@Nullable
  private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr)