private void processPrimaryConstructor( @NotNull TopDownAnalysisContext c, @NotNull MutableClassDescriptor classDescriptor, @NotNull JetClass klass) { // TODO : not all the parameters are real properties JetScope memberScope = classDescriptor.getScopeForClassHeaderResolution(); ConstructorDescriptor constructorDescriptor = descriptorResolver.resolvePrimaryConstructorDescriptor( memberScope, classDescriptor, klass, trace); if (constructorDescriptor != null) { List<ValueParameterDescriptor> valueParameterDescriptors = constructorDescriptor.getValueParameters(); List<JetParameter> primaryConstructorParameters = klass.getPrimaryConstructorParameters(); assert valueParameterDescriptors.size() == primaryConstructorParameters.size(); List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>(); for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) { JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex()); if (parameter.getValOrVarNode() != null) { PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePrimaryConstructorParameterToAProperty( classDescriptor, valueParameterDescriptor, memberScope, parameter, trace); classDescriptor.getBuilder().addPropertyDescriptor(propertyDescriptor); c.getPrimaryConstructorParameterProperties().put(parameter, propertyDescriptor); } else { notProperties.add(valueParameterDescriptor); } } if (classDescriptor.getKind() != ClassKind.TRAIT) { classDescriptor.setPrimaryConstructor(constructorDescriptor); classDescriptor.addConstructorParametersToInitializersScope(notProperties); } } }
private void createClassObjectForEnumClass( @NotNull MutableClassDescriptor mutableClassDescriptor) { if (mutableClassDescriptor.getKind() == ClassKind.ENUM_CLASS) { MutableClassDescriptor classObject = createSyntheticClassObject(mutableClassDescriptor); mutableClassDescriptor.getBuilder().setClassObjectDescriptor(classObject); classObject .getBuilder() .addFunctionDescriptor( DescriptorResolver.createEnumClassObjectValuesMethod(classObject, trace)); classObject .getBuilder() .addFunctionDescriptor( DescriptorResolver.createEnumClassObjectValueOfMethod(classObject, trace)); } }
@NotNull private MutablePackageFragmentDescriptor getOrCreatePackageFragmentForFile( @NotNull JetFile file) { JetPackageDirective packageDirective = file.getPackageDirective(); assert packageDirective != null : "scripts are not supported"; MutablePackageFragmentDescriptor fragment = packageFragmentProvider.getOrCreateFragment(packageDirective.getFqName()); ModuleDescriptor module = packageFragmentProvider.getModule(); DescriptorResolver.resolvePackageHeader( packageDirective, module, TypeHierarchyResolver.this.trace); trace.record(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file, fragment); // Register files corresponding to this package // The trace currently does not support bi-di multimaps that would handle this task nicer FqName fqName = fragment.getFqName(); Collection<JetFile> files = trace.get(PACKAGE_TO_FILES, fqName); if (files == null) { files = Sets.newIdentityHashSet(); } files.add(file); trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files); return fragment; }
private void resolvePropertyInitializer( JetProperty property, PropertyDescriptor propertyDescriptor, JetExpression initializer, JetScope scope) { // JetFlowInformationProvider flowInformationProvider = // context.getDescriptorResolver().computeFlowData(property, initializer); // TODO : flow JET-15 JetType expectedTypeForInitializer = property.getPropertyTypeRef() != null ? propertyDescriptor.getType() : NO_EXPECTED_TYPE; JetType type = expressionTypingServices.getType( descriptorResolver.getPropertyDeclarationInnerScope( scope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter(), trace), initializer, expectedTypeForInitializer, trace); // // JetType expectedType = propertyDescriptor.getInType(); // if (expectedType == null) { // expectedType = propertyDescriptor.getType(); // } // if (type != null && expectedType != null // && !context.getSemanticServices().getTypeChecker().isSubtypeOf(type, // expectedType)) { //// trace.report(TYPE_MISMATCH.on(initializer, expectedType, type)); // } }
private void checkFinalUpperBounds(@Nullable JetTypeReference typeReference) { if (typeReference != null) { JetType type = trace.getBindingContext().get(TYPE, typeReference); if (type != null) { DescriptorResolver.checkUpperBoundType(typeReference, type, trace); } } }
private void checkBoundsForTypeInClassHeader(@Nullable JetTypeReference typeReference) { if (typeReference != null) { JetType type = trace.getBindingContext().get(TYPE, typeReference); if (type != null) { DescriptorResolver.checkBounds(typeReference, type, trace); } } }
@NotNull private ConstructorDescriptorImpl createPrimaryConstructorForObject( @Nullable PsiElement object, @NotNull MutableClassDescriptor mutableClassDescriptor) { ConstructorDescriptorImpl constructorDescriptor = DescriptorResolver.createAndRecordPrimaryConstructorForObject( object, mutableClassDescriptor, trace); mutableClassDescriptor.setPrimaryConstructor(constructorDescriptor); return constructorDescriptor; }
private void createCopyFunction( @NotNull MutableClassDescriptor classDescriptor, @NotNull ConstructorDescriptor constructorDescriptor) { SimpleFunctionDescriptor functionDescriptor = DescriptorResolver.createCopyFunctionDescriptor( constructorDescriptor.getValueParameters(), classDescriptor, trace); classDescriptor.getBuilder().addFunctionDescriptor(functionDescriptor); }
public void process(@NotNull BodiesResolveContext bodiesResolveContext) { for (JetFile file : bodiesResolveContext.getFiles()) { checkModifiersAndAnnotationsInPackageDirective(file); AnnotationTargetChecker.INSTANCE$.check(file, trace); } Map<JetClassOrObject, ClassDescriptorWithResolutionScopes> classes = bodiesResolveContext.getDeclaredClasses(); for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry : classes.entrySet()) { JetClassOrObject classOrObject = entry.getKey(); ClassDescriptorWithResolutionScopes classDescriptor = entry.getValue(); checkSupertypesForConsistency(classDescriptor); checkTypesInClassHeader(classOrObject); if (classOrObject instanceof JetClass) { JetClass jetClass = (JetClass) classOrObject; checkClass(bodiesResolveContext, jetClass, classDescriptor); descriptorResolver.checkNamesInConstraints( jetClass, classDescriptor, classDescriptor.getScopeForClassHeaderResolution(), trace); } else if (classOrObject instanceof JetObjectDeclaration) { checkObject((JetObjectDeclaration) classOrObject, classDescriptor); } checkPrimaryConstructor(classOrObject, classDescriptor); modifiersChecker.checkModifiersForDeclaration(classOrObject, classDescriptor); } Map<JetNamedFunction, SimpleFunctionDescriptor> functions = bodiesResolveContext.getFunctions(); for (Map.Entry<JetNamedFunction, SimpleFunctionDescriptor> entry : functions.entrySet()) { JetNamedFunction function = entry.getKey(); SimpleFunctionDescriptor functionDescriptor = entry.getValue(); checkFunction(function, functionDescriptor); modifiersChecker.checkModifiersForDeclaration(function, functionDescriptor); } Map<JetProperty, PropertyDescriptor> properties = bodiesResolveContext.getProperties(); for (Map.Entry<JetProperty, PropertyDescriptor> entry : properties.entrySet()) { JetProperty property = entry.getKey(); PropertyDescriptor propertyDescriptor = entry.getValue(); checkProperty(property, propertyDescriptor); modifiersChecker.checkModifiersForDeclaration(property, propertyDescriptor); } for (Map.Entry<JetSecondaryConstructor, ConstructorDescriptor> entry : bodiesResolveContext.getSecondaryConstructors().entrySet()) { ConstructorDescriptor constructorDescriptor = entry.getValue(); JetSecondaryConstructor declaration = entry.getKey(); checkConstructorDeclaration(constructorDescriptor, declaration); } }
private void checkTypeParameters(JetTypeParameterListOwner typeParameterListOwner) { // TODO: Support annotation for type parameters for (JetTypeParameter jetTypeParameter : typeParameterListOwner.getTypeParameters()) { AnnotationResolver.reportUnsupportedAnnotationForTypeParameter(jetTypeParameter, trace); TypeParameterDescriptor typeParameter = trace.get(TYPE_PARAMETER, jetTypeParameter); if (typeParameter != null) { DescriptorResolver.checkConflictingUpperBounds(trace, typeParameter, jetTypeParameter); } AnnotationTargetChecker.INSTANCE$.check(jetTypeParameter, trace); } }
private void resolveTypesInClassHeaders(@NotNull TopDownAnalysisContext c) { for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry : c.getClasses().entrySet()) { JetClassOrObject classOrObject = entry.getKey(); if (classOrObject instanceof JetClass) { ClassDescriptorWithResolutionScopes descriptor = entry.getValue(); //noinspection unchecked descriptorResolver.resolveGenericBounds( (JetClass) classOrObject, descriptor, descriptor.getScopeForClassHeaderResolution(), (List) descriptor.getTypeConstructor().getParameters(), trace); } } for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry : c.getClasses().entrySet()) { descriptorResolver.resolveSupertypesForMutableClassDescriptor( entry.getKey(), (MutableClassDescriptor) entry.getValue(), trace); } }
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(); } } }
private void checkDeclaredTypeInPublicMember( JetNamedDeclaration member, CallableMemberDescriptor memberDescriptor) { boolean hasDeferredType; if (member instanceof JetProperty) { hasDeferredType = ((JetProperty) member).getTypeReference() == null && DescriptorResolver.hasBody((JetProperty) member); } else { assert member instanceof JetFunction; JetFunction function = (JetFunction) member; hasDeferredType = function.getTypeReference() == null && function.hasBody() && !function.hasBlockBody(); } if ((memberDescriptor.getVisibility().isPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) { trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member)); } }
private void createComponentFunctions( @NotNull MutableClassDescriptor classDescriptor, @NotNull ConstructorDescriptor constructorDescriptor) { int parameterIndex = 0; for (ValueParameterDescriptor parameter : constructorDescriptor.getValueParameters()) { if (!parameter.getType().isError()) { PropertyDescriptor property = trace.get(BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameter); if (property != null) { ++parameterIndex; SimpleFunctionDescriptor functionDescriptor = DescriptorResolver.createComponentFunctionDescriptor( parameterIndex, property, parameter, classDescriptor, trace); classDescriptor.getBuilder().addFunctionDescriptor(functionDescriptor); } } } }
private JetScope makeScopeForPropertyAccessor( @NotNull JetPropertyAccessor accessor, PropertyDescriptor propertyDescriptor) { JetScope declaringScope = context.getDeclaringScopes().get(accessor); JetScope propertyDeclarationInnerScope = descriptorResolver.getPropertyDeclarationInnerScope( declaringScope, propertyDescriptor, propertyDescriptor.getTypeParameters(), propertyDescriptor.getReceiverParameter(), trace); WritableScope accessorScope = new WritableScopeImpl( propertyDeclarationInnerScope, declaringScope.getContainingDeclaration(), new TraceBasedRedeclarationHandler(trace)) .setDebugName("Accessor scope"); accessorScope.changeLockLevel(WritableScope.LockLevel.READING); return accessorScope; }