@NotNull
 public static JetType getDefaultType(IElementType constantType) {
   if (constantType == JetNodeTypes.INTEGER_CONSTANT) {
     return JetStandardLibrary.getInstance().getIntType();
   } else if (constantType == JetNodeTypes.FLOAT_CONSTANT) {
     return JetStandardLibrary.getInstance().getDoubleType();
   } else if (constantType == JetNodeTypes.BOOLEAN_CONSTANT) {
     return JetStandardLibrary.getInstance().getBooleanType();
   } else if (constantType == JetNodeTypes.CHARACTER_CONSTANT) {
     return JetStandardLibrary.getInstance().getCharType();
   } else if (constantType == JetNodeTypes.NULL) {
     return JetStandardClasses.getNullableNothingType();
   } else {
     throw new IllegalArgumentException("Unsupported constant type: " + constantType);
   }
 }
示例#2
0
 private Map<FqName, ClassDescriptor> getPrimitiveWrappersClassDescriptorMap() {
   if (classDescriptorMap == null) {
     classDescriptorMap = new HashMap<FqName, ClassDescriptor>();
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType();
       classDescriptorMap.put(
           jvmPrimitiveType.getWrapper().getFqName(),
           JetStandardLibrary.getInstance().getPrimitiveClassDescriptor(primitiveType));
     }
     classDescriptorMap.put(
         new FqName("java.lang.String"), JetStandardLibrary.getInstance().getString());
     classDescriptorMap.put(
         new FqName("java.lang.CharSequence"), JetStandardLibrary.getInstance().getCharSequence());
     classDescriptorMap.put(
         new FqName("java.lang.Throwable"), JetStandardLibrary.getInstance().getThrowable());
   }
   return classDescriptorMap;
 }
示例#3
0
 public Map<String, JetType> getClassTypesMap() {
   if (classTypesMap == null) {
     classTypesMap = new HashMap<String, JetType>();
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType();
       classTypesMap.put(
           jvmPrimitiveType.getWrapper().getFqName().getFqName(),
           JetStandardLibrary.getInstance().getNullablePrimitiveJetType(primitiveType));
     }
     classTypesMap.put("java.lang.Object", JetStandardClasses.getNullableAnyType());
     classTypesMap.put(
         "java.lang.String", JetStandardLibrary.getInstance().getNullableStringType());
     classTypesMap.put(
         "java.lang.CharSequence", JetStandardLibrary.getInstance().getNullableCharSequenceType());
     classTypesMap.put(
         "java.lang.Throwable", JetStandardLibrary.getInstance().getNullableThrowableType());
   }
   return classTypesMap;
 }
示例#4
0
 public Map<String, JetType> getPrimitiveTypesMap() {
   if (primitiveTypesMap == null) {
     primitiveTypesMap = new HashMap<String, JetType>();
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType();
       primitiveTypesMap.put(
           jvmPrimitiveType.getName(),
           JetStandardLibrary.getInstance().getPrimitiveJetType(primitiveType));
       primitiveTypesMap.put(
           "[" + jvmPrimitiveType.getName(),
           JetStandardLibrary.getInstance().getPrimitiveArrayJetType(primitiveType));
       primitiveTypesMap.put(
           jvmPrimitiveType.getWrapper().getFqName().getFqName(),
           JetStandardLibrary.getInstance().getNullablePrimitiveJetType(primitiveType));
     }
     primitiveTypesMap.put("void", JetStandardClasses.getUnitType());
   }
   return primitiveTypesMap;
 }
 private JetType getPrimitiveType(char descriptor, boolean nullable) {
   if (!nullable) {
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       if (jvmPrimitiveType.getJvmLetter() == descriptor) {
         return jetStandardLibrary.getPrimitiveJetType(jvmPrimitiveType.getPrimitiveType());
       }
     }
     if (descriptor == 'V') {
       return JetStandardClasses.getUnitType();
     }
   } else {
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       if (jvmPrimitiveType.getJvmLetter() == descriptor) {
         return jetStandardLibrary.getNullablePrimitiveJetType(
             jvmPrimitiveType.getPrimitiveType());
       }
     }
     if (descriptor == 'V') {
       throw new IllegalStateException("incorrect signature: nullable void");
     }
   }
   throw new IllegalStateException("incorrect signature");
 }
示例#6
0
 @NotNull
 public JetType transformToType(
     @NotNull String kotlinSignature, TypeVariableResolver typeVariableResolver) {
   final JetType[] r = new JetType[1];
   JetTypeJetSignatureReader reader =
       new JetTypeJetSignatureReader(
           javaSemanticServices, JetStandardLibrary.getInstance(), typeVariableResolver) {
         @Override
         protected void done(@NotNull JetType jetType) {
           r[0] = jetType;
         }
       };
   new JetSignatureReader(kotlinSignature).acceptType(reader);
   return r[0];
 }
 public static boolean ensureBooleanResultWithCustomSubject(
     JetExpression operationSign,
     JetType resultType,
     String subjectName,
     ExpressionTypingContext context) {
   if (resultType != null) {
     // TODO : Relax?
     if (!isBoolean(resultType)) {
       context.trace.report(
           RESULT_TYPE_MISMATCH.on(
               operationSign,
               subjectName,
               JetStandardLibrary.getInstance().getBooleanType(),
               resultType));
       return false;
     }
   }
   return true;
 }
示例#8
0
 @Override
 public StackValue generate(
     ExpressionCodegen codegen,
     InstructionAdapter v,
     @NotNull Type expectedType,
     PsiElement element,
     List<JetExpression> arguments,
     StackValue receiver,
     @NotNull GenerationState state) {
   receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
   JetCallExpression call = (JetCallExpression) element;
   FunctionDescriptor funDescriptor =
       (FunctionDescriptor)
           codegen
               .getBindingContext()
               .get(
                   BindingContext.REFERENCE_TARGET,
                   (JetSimpleNameExpression) call.getCalleeExpression());
   assert funDescriptor != null;
   ClassDescriptor containingDeclaration =
       (ClassDescriptor) funDescriptor.getContainingDeclaration().getOriginal();
   if (containingDeclaration.equals(JetStandardLibrary.getInstance().getArray())) {
     v.invokestatic(
         "jet/runtime/ArrayIterator", "iterator", "([Ljava/lang/Object;)Ljava/util/Iterator;");
     return StackValue.onStack(AsmTypeConstants.JET_ITERATOR_TYPE);
   } else {
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType();
       if (primitiveType.getArrayClassName().is(containingDeclaration)) {
         String methodSignature =
             "(["
                 + jvmPrimitiveType.getJvmLetter()
                 + ")"
                 + jvmPrimitiveType.getIterator().getDescriptor();
         v.invokestatic("jet/runtime/ArrayIterator", "iterator", methodSignature);
         return StackValue.onStack(jvmPrimitiveType.getIterator().getAsmType());
       }
     }
     throw new UnsupportedOperationException(containingDeclaration.toString());
   }
 }
  public static AnalyzeExhaust analyzeFilesWithJavaIntegration(
      Project project,
      Collection<JetFile> files,
      Predicate<PsiFile> filesToAnalyzeCompletely,
      JetControlFlowDataTraceFactory flowDataTraceFactory) {
    BindingTraceContext bindingTraceContext = new BindingTraceContext();

    final ModuleDescriptor owner = new ModuleDescriptor("<module>");

    TopDownAnalysisParameters topDownAnalysisParameters =
        new TopDownAnalysisParameters(filesToAnalyzeCompletely, false, false);

    InjectorForTopDownAnalyzerForJvm injector =
        new InjectorForTopDownAnalyzerForJvm(
            project,
            topDownAnalysisParameters,
            new ObservableBindingTrace(bindingTraceContext),
            owner,
            flowDataTraceFactory);

    injector.getTopDownAnalyzer().analyzeFiles(files);
    return new AnalyzeExhaust(
        bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance());
  }
示例#10
0
 private static void addNamesForType(
     ArrayList<String> result, JetType jetType, JetNameValidator validator) {
   JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
   JetTypeChecker typeChecker = JetTypeChecker.INSTANCE;
   if (ErrorUtils.containsErrorType(jetType)) return;
   if (typeChecker.equalTypes(standardLibrary.getBooleanType(), jetType)) {
     addName(result, "b", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getIntType(), jetType)) {
     addName(result, "i", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getByteType(), jetType)) {
     addName(result, "byte", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getLongType(), jetType)) {
     addName(result, "l", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getFloatType(), jetType)) {
     addName(result, "fl", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getDoubleType(), jetType)) {
     addName(result, "d", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getShortType(), jetType)) {
     addName(result, "sh", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getCharType(), jetType)) {
     addName(result, "c", validator);
   } else if (typeChecker.equalTypes(standardLibrary.getStringType(), jetType)) {
     addName(result, "s", validator);
   } else {
     if (jetType.getArguments().size() == 1) {
       JetType argument = jetType.getArguments().get(0).getType();
       if (typeChecker.equalTypes(standardLibrary.getArrayType(argument), jetType)) {
         if (typeChecker.equalTypes(standardLibrary.getBooleanType(), argument)) {
           addName(result, "booleans", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getIntType(), argument)) {
           addName(result, "ints", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getByteType(), argument)) {
           addName(result, "bytes", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getLongType(), argument)) {
           addName(result, "longs", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getFloatType(), argument)) {
           addName(result, "floats", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getDoubleType(), argument)) {
           addName(result, "doubles", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getShortType(), argument)) {
           addName(result, "shorts", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getCharType(), argument)) {
           addName(result, "chars", validator);
         } else if (typeChecker.equalTypes(standardLibrary.getStringType(), argument)) {
           addName(result, "strings", validator);
         } else {
           ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(argument);
           if (classDescriptor != null) {
             Name className = classDescriptor.getName();
             addName(
                 result, "arrayOf" + StringUtil.capitalize(className.getName()) + "s", validator);
           }
         }
       } else {
         addForClassType(result, jetType, validator);
       }
     } else {
       addForClassType(result, jetType, validator);
     }
   }
 }
示例#11
0
 public static boolean isBoolean(@NotNull JetType type) {
   return JetTypeChecker.INSTANCE.isSubtypeOf(
       type, JetStandardLibrary.getInstance().getBooleanType());
 }
示例#12
0
  @Override
  protected ExpectedResolveData getExpectedResolveData() {
    Project project = getProject();
    JetStandardLibrary lib = JetStandardLibrary.getInstance();
    Map<String, DeclarationDescriptor> nameToDescriptor =
        new HashMap<String, DeclarationDescriptor>();
    nameToDescriptor.put(
        "kotlin::Int.plus(Int)", standardFunction(lib.getInt(), "plus", lib.getIntType()));
    FunctionDescriptor descriptorForGet =
        standardFunction(
            lib.getArray(),
            Collections.singletonList(new TypeProjection(lib.getIntType())),
            "get",
            lib.getIntType());
    nameToDescriptor.put("kotlin::Array.get(Int)", descriptorForGet.getOriginal());
    nameToDescriptor.put(
        "kotlin::Int.compareTo(Double)",
        standardFunction(lib.getInt(), "compareTo", lib.getDoubleType()));
    @NotNull
    FunctionDescriptor descriptorForSet =
        standardFunction(
            lib.getArray(),
            Collections.singletonList(new TypeProjection(lib.getIntType())),
            "set",
            lib.getIntType(),
            lib.getIntType());
    nameToDescriptor.put("kotlin::Array.set(Int, Int)", descriptorForSet.getOriginal());

    Map<String, PsiElement> nameToDeclaration = new HashMap<String, PsiElement>();
    PsiClass java_util_Collections = findClass("java.util.Collections");
    nameToDeclaration.put(
        "java::java.util.Collections.emptyList()", findMethod(java_util_Collections, "emptyList"));
    nameToDeclaration.put("java::java.util.Collections", java_util_Collections);
    PsiClass java_util_List = findClass("java.util.List");
    nameToDeclaration.put("java::java.util.List", java_util_List);
    nameToDeclaration.put(
        "java::java.util.List.set()", java_util_List.findMethodsByName("set", true)[0]);
    nameToDeclaration.put(
        "java::java.util.List.get()", java_util_List.findMethodsByName("get", true)[0]);
    nameToDeclaration.put("java::java", findPackage("java"));
    nameToDeclaration.put("java::java.util", findPackage("java.util"));
    nameToDeclaration.put("java::java.lang", findPackage("java.lang"));
    nameToDeclaration.put("java::java.lang.Object", findClass("java.lang.Object"));
    PsiClass java_lang_System = findClass("java.lang.System");
    nameToDeclaration.put("java::java.lang.System", java_lang_System);
    PsiMethod[] methods = findClass("java.io.PrintStream").findMethodsByName("print", true);
    nameToDeclaration.put("java::java.io.PrintStream.print(Object)", methods[8]);
    nameToDeclaration.put("java::java.io.PrintStream.print(Int)", methods[2]);
    nameToDeclaration.put("java::java.io.PrintStream.print(char[])", methods[6]);
    nameToDeclaration.put("java::java.io.PrintStream.print(Double)", methods[5]);
    nameToDeclaration.put(
        "java::java.lang.System.out", java_lang_System.findFieldByName("out", true));
    PsiClass java_lang_Number = findClass("java.lang.Number");
    nameToDeclaration.put("java::java.lang.Number", java_lang_Number);
    nameToDeclaration.put(
        "java::java.lang.Number.intValue()",
        java_lang_Number.findMethodsByName("intValue", true)[0]);

    return new ExpectedResolveData(nameToDescriptor, nameToDeclaration) {
      @Override
      protected JetFile createJetFile(String fileName, String text) {
        return createCheckAndReturnPsiFile(fileName, text);
      }
    };
  }