Пример #1
0
  public static void reportCyclicInheritanceHierarchyError(
      @NotNull BindingTrace trace,
      @NotNull ClassDescriptor classDescriptor,
      @NotNull ClassDescriptor superclass) {
    PsiElement psiElement =
        BindingContextUtils.classDescriptorToDeclaration(
            trace.getBindingContext(), classDescriptor);

    PsiElement elementToMark = null;
    if (psiElement instanceof JetClassOrObject) {
      JetClassOrObject classOrObject = (JetClassOrObject) psiElement;
      for (JetDelegationSpecifier delegationSpecifier : classOrObject.getDelegationSpecifiers()) {
        JetTypeReference typeReference = delegationSpecifier.getTypeReference();
        if (typeReference == null) continue;
        JetType supertype = trace.get(TYPE, typeReference);
        if (supertype != null && supertype.getConstructor() == superclass.getTypeConstructor()) {
          elementToMark = typeReference;
        }
      }
    }
    if (elementToMark == null && psiElement instanceof PsiNameIdentifierOwner) {
      PsiNameIdentifierOwner namedElement = (PsiNameIdentifierOwner) psiElement;
      PsiElement nameIdentifier = namedElement.getNameIdentifier();
      if (nameIdentifier != null) {
        elementToMark = nameIdentifier;
      }
    }
    if (elementToMark != null) {
      trace.report(CYCLIC_INHERITANCE_HIERARCHY.on(elementToMark));
    }
  }
Пример #2
0
  private void resolveFunctionAndPropertyHeaders(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetFile, WritableScope> entry : c.getFileScopes().entrySet()) {
      JetFile file = entry.getKey();
      WritableScope fileScope = entry.getValue();
      PackageLikeBuilder packageBuilder = c.getPackageFragments().get(file).getBuilder();

      resolveFunctionAndPropertyHeaders(
          c, file.getDeclarations(), fileScope, fileScope, fileScope, packageBuilder);
    }
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      MutableClassDescriptor classDescriptor = (MutableClassDescriptor) entry.getValue();

      resolveFunctionAndPropertyHeaders(
          c,
          classOrObject.getDeclarations(),
          classDescriptor.getScopeForMemberDeclarationResolution(),
          classDescriptor.getScopeForInitializerResolution(),
          classDescriptor.getScopeForMemberDeclarationResolution(),
          classDescriptor.getBuilder());
    }

    // TODO : Extensions
  }
Пример #3
0
  public static void genClassOrObject(
      @NotNull CodegenContext parentContext,
      @NotNull JetClassOrObject aClass,
      @NotNull GenerationState state,
      @Nullable MemberCodegen<?> parentCodegen) {
    ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);

    if (descriptor == null || ErrorUtils.isError(descriptor)) {
      badDescriptor(descriptor, state.getClassBuilderMode());
      return;
    }

    if (descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED)) {
      badDescriptor(descriptor, state.getClassBuilderMode());
    }

    ClassBuilder classBuilder =
        state.getFactory().forClassImplementation(descriptor, aClass.getContainingFile());
    ClassContext classContext =
        parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION, state);
    new ImplementationBodyCodegen(aClass, classContext, classBuilder, state, parentCodegen)
        .generate();

    if (aClass instanceof JetClass && ((JetClass) aClass).isTrait()) {
      ClassBuilder traitImplBuilder =
          state.getFactory().forTraitImplementation(descriptor, state, aClass.getContainingFile());
      ClassContext traitImplContext =
          parentContext.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state);
      new TraitImplBodyCodegen(aClass, traitImplContext, traitImplBuilder, state, parentCodegen)
          .generate();
    }
  }
Пример #4
0
  private void createFunctionsForDataClasses(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      JetClassOrObject klass = entry.getKey();
      MutableClassDescriptor classDescriptor = (MutableClassDescriptor) entry.getValue();

      if (klass instanceof JetClass
          && klass.hasPrimaryConstructor()
          && KotlinBuiltIns.getInstance().isData(classDescriptor)) {
        ConstructorDescriptor constructor =
            DescriptorUtils.getConstructorOfDataClass(classDescriptor);
        createComponentFunctions(classDescriptor, constructor);
        createCopyFunction(classDescriptor, constructor);
      }
    }
  }
Пример #5
0
  private void createTypeConstructors(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      MutableClassDescriptor descriptor = (MutableClassDescriptor) entry.getValue();
      if (classOrObject instanceof JetClass) {
        descriptorResolver.resolveMutableClassDescriptor(
            (JetClass) classOrObject, descriptor, trace);
      } else if (classOrObject instanceof JetObjectDeclaration) {
        descriptor.setModality(Modality.FINAL);
        descriptor.setVisibility(
            resolveVisibilityFromModifiers(classOrObject, getDefaultClassVisibility(descriptor)));
        descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
      }

      descriptor.createTypeConstructor();

      ClassKind kind = descriptor.getKind();
      if (kind == ClassKind.ENUM_ENTRY
          || kind == ClassKind.OBJECT
          || kind == ClassKind.ENUM_CLASS) {
        MutableClassDescriptorLite classObject = descriptor.getClassObjectDescriptor();
        assert classObject != null
            : "Enum entries and named objects should have class objects: "
                + classOrObject.getText();

        JetType supertype;
        if (kind == ClassKind.ENUM_CLASS) {
          supertype = KotlinBuiltIns.getInstance().getAnyType();
        } else {
          // This is a clever hack: each enum entry and object declaration (i.e. singleton) has a
          // synthetic class object.
          // We make this class object inherit from the singleton here, thus allowing to use the
          // singleton's class object where
          // the instance of the singleton is applicable. Effectively all members of the singleton
          // would be present in its class
          // object as fake overrides, so you can access them via standard class object notation:
          // ObjectName.memberName()
          supertype = descriptor.getDefaultType();
        }
        classObject.setSupertypes(Collections.singleton(supertype));
        classObject.createTypeConstructor();
      }
    }
  }
Пример #6
0
 private void resolveAnnotationsForClassOrObject(
     AnnotationResolver annotationResolver,
     JetClassOrObject jetClass,
     MutableClassDescriptor descriptor) {
   JetModifierList modifierList = jetClass.getModifierList();
   if (modifierList != null) {
     descriptor
         .getAnnotations()
         .addAll(
             annotationResolver.resolveAnnotations(
                 descriptor.getScopeForSupertypeResolution(),
                 modifierList.getAnnotationEntries()));
   }
 }
Пример #7
0
 private void resolveAnonymousInitializers(
     JetClassOrObject jetClassOrObject, MutableClassDescriptor classDescriptor) {
   if (!context.completeAnalysisNeeded(jetClassOrObject)) return;
   List<JetClassInitializer> anonymousInitializers = jetClassOrObject.getAnonymousInitializers();
   if (classDescriptor.getUnsubstitutedPrimaryConstructor() != null) {
     ConstructorDescriptor primaryConstructor =
         classDescriptor.getUnsubstitutedPrimaryConstructor();
     assert primaryConstructor != null;
     final JetScope scopeForInitializers = classDescriptor.getScopeForInitializers();
     for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
       expressionTypingServices.getType(
           scopeForInitializers, anonymousInitializer.getBody(), NO_EXPECTED_TYPE, trace);
     }
   } else {
     for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
       trace.report(ANONYMOUS_INITIALIZER_WITHOUT_CONSTRUCTOR.on(anonymousInitializer));
     }
   }
 }
Пример #8
0
 public static boolean isNonLiteralObject(JetClassOrObject myClass) {
   return myClass instanceof JetObjectDeclaration
       && !((JetObjectDeclaration) myClass).isObjectLiteral()
       && !(myClass.getParent() instanceof JetClassObject);
 }
Пример #9
0
  private void resolveDelegationSpecifierList(
      final JetClassOrObject jetClass, final MutableClassDescriptor descriptor) {
    if (!context.completeAnalysisNeeded(jetClass)) return;
    final ConstructorDescriptor primaryConstructor =
        descriptor.getUnsubstitutedPrimaryConstructor();
    final JetScope scopeForConstructor =
        primaryConstructor == null
            ? null
            : FunctionDescriptorUtil.getFunctionInnerScope(
                descriptor.getScopeForSupertypeResolution(), primaryConstructor, trace);
    final ExpressionTypingServices typeInferrer = expressionTypingServices; // TODO : flow

    final Map<JetTypeReference, JetType> supertypes = Maps.newLinkedHashMap();
    JetVisitorVoid visitor =
        new JetVisitorVoid() {
          private void recordSupertype(JetTypeReference typeReference, JetType supertype) {
            if (supertype == null) return;
            supertypes.put(typeReference, supertype);
          }

          @Override
          public void visitDelegationByExpressionSpecifier(
              JetDelegatorByExpressionSpecifier specifier) {
            if (descriptor.getKind() == ClassKind.TRAIT) {
              trace.report(DELEGATION_IN_TRAIT.on(specifier));
            }
            JetType supertype =
                trace.getBindingContext().get(BindingContext.TYPE, specifier.getTypeReference());
            recordSupertype(specifier.getTypeReference(), supertype);
            if (supertype != null) {
              DeclarationDescriptor declarationDescriptor =
                  supertype.getConstructor().getDeclarationDescriptor();
              if (declarationDescriptor instanceof ClassDescriptor) {
                ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
                if (classDescriptor.getKind() != ClassKind.TRAIT) {
                  trace.report(DELEGATION_NOT_TO_TRAIT.on(specifier.getTypeReference()));
                }
              }
            }
            JetExpression delegateExpression = specifier.getDelegateExpression();
            if (delegateExpression != null) {
              JetScope scope =
                  scopeForConstructor == null
                      ? descriptor.getScopeForMemberResolution()
                      : scopeForConstructor;
              JetType type =
                  typeInferrer.getType(scope, delegateExpression, NO_EXPECTED_TYPE, trace);
              if (type != null
                  && supertype != null
                  && !JetTypeChecker.INSTANCE.isSubtypeOf(type, supertype)) {
                trace.report(TYPE_MISMATCH.on(delegateExpression, supertype, type));
              }
            }
          }

          @Override
          public void visitDelegationToSuperCallSpecifier(JetDelegatorToSuperCall call) {
            JetValueArgumentList valueArgumentList = call.getValueArgumentList();
            PsiElement elementToMark = valueArgumentList == null ? call : valueArgumentList;
            if (descriptor.getKind() == ClassKind.TRAIT) {
              trace.report(SUPERTYPE_INITIALIZED_IN_TRAIT.on(elementToMark));
            }
            JetTypeReference typeReference = call.getTypeReference();
            if (typeReference == null) return;
            if (descriptor.getUnsubstitutedPrimaryConstructor() == null) {
              assert descriptor.getKind() == ClassKind.TRAIT;
              return;
            }
            OverloadResolutionResults<FunctionDescriptor> results =
                callResolver.resolveFunctionCall(
                    trace,
                    scopeForConstructor,
                    CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call),
                    NO_EXPECTED_TYPE,
                    DataFlowInfo.EMPTY);
            if (results.isSuccess()) {
              JetType supertype = results.getResultingDescriptor().getReturnType();
              recordSupertype(typeReference, supertype);
              ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
              if (classDescriptor != null) {
                if (classDescriptor.getKind() == ClassKind.TRAIT) {
                  trace.report(CONSTRUCTOR_IN_TRAIT.on(elementToMark));
                }
              }
            } else {
              recordSupertype(
                  typeReference, trace.getBindingContext().get(BindingContext.TYPE, typeReference));
            }
          }

          @Override
          public void visitDelegationToSuperClassSpecifier(JetDelegatorToSuperClass specifier) {
            JetTypeReference typeReference = specifier.getTypeReference();
            JetType supertype = trace.getBindingContext().get(BindingContext.TYPE, typeReference);
            recordSupertype(typeReference, supertype);
            if (supertype == null) return;
            ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
            if (classDescriptor == null) return;
            if (descriptor.getKind() != ClassKind.TRAIT
                && classDescriptor.hasConstructors()
                && !ErrorUtils.isError(classDescriptor.getTypeConstructor())
                && classDescriptor.getKind() != ClassKind.TRAIT) {
              boolean hasConstructorWithoutParams = false;
              for (ConstructorDescriptor constructor : classDescriptor.getConstructors()) {
                if (constructor.getValueParameters().isEmpty()) {
                  hasConstructorWithoutParams = true;
                }
              }
              if (!hasConstructorWithoutParams) {
                trace.report(SUPERTYPE_NOT_INITIALIZED.on(specifier));
              } else {
                trace.report(SUPERTYPE_NOT_INITIALIZED_DEFAULT.on(specifier));
              }
            }
          }

          @Override
          public void visitDelegationToThisCall(JetDelegatorToThisCall thisCall) {
            throw new IllegalStateException("This-calls should be prohibited by the parser");
          }

          @Override
          public void visitJetElement(JetElement element) {
            throw new UnsupportedOperationException(element.getText() + " : " + element);
          }
        };

    for (JetDelegationSpecifier delegationSpecifier : jetClass.getDelegationSpecifiers()) {
      delegationSpecifier.accept(visitor);
    }

    Set<TypeConstructor> parentEnum = Collections.emptySet();
    if (jetClass instanceof JetEnumEntry) {
      parentEnum =
          Collections.singleton(
              ((ClassDescriptor) descriptor.getContainingDeclaration().getContainingDeclaration())
                  .getTypeConstructor());
    }

    checkSupertypeList(descriptor, supertypes, parentEnum);
  }