public TypeSymbol GetDeclaredType(IBinder binder)
        {
            if (binder.GetSymbolInfo(this) is not ModuleSymbol moduleSymbol)
            {
                // TODO: Ideally we'd still be able to return a type here, but we'd need access to the compilation to get it.
                return(ErrorType.Empty());
            }

            if (!moduleSymbol.TryGetSemanticModel(out var moduleSemanticModel, out var failureDiagnostic))
            {
                return(ErrorType.Create(failureDiagnostic));
            }

            return(LanguageConstants.CreateModuleType(
                       moduleSemanticModel.ParameterTypeProperties,
                       moduleSemanticModel.OutputTypeProperties,
                       moduleSemanticModel.TargetScope,
                       binder.TargetScope,
                       LanguageConstants.TypeNameModule));
        }
Пример #2
0
        public TypeSymbol GetAssignedType(ITypeManager typeManager, ArraySyntax?allowedSyntax)
        {
            var assignedType = typeManager.GetDeclaredType(this);

            if (assignedType is null)
            {
                // We don't expect this to happen for a parameter.
                return(ErrorType.Empty());
            }

            // TODO: remove SyntaxHelper.TryGetAllowedSyntax when we drop parameter modifiers support.
            if (allowedSyntax is not null && !allowedSyntax.Items.Any())
            {
                return(ErrorType.Create(DiagnosticBuilder.ForPosition(allowedSyntax).AllowedMustContainItems()));
            }

            var allowedItemTypes = allowedSyntax?.Items.Select(typeManager.GetTypeInfo);

            if (ReferenceEquals(assignedType, LanguageConstants.String))
            {
                if (allowedItemTypes?.All(itemType => itemType is StringLiteralType) == true)
                {
                    assignedType = TypeHelper.CreateTypeUnion(allowedItemTypes);
                }
                else
                {
                    // In order to support assignment for a generic string to enum-typed properties (which generally is forbidden),
                    // we need to relax the validation for string parameters without 'allowed' values specified.
                    assignedType = LanguageConstants.LooseString;
                }
            }

            if (ReferenceEquals(assignedType, LanguageConstants.Array) &&
                allowedItemTypes?.All(itemType => itemType is StringLiteralType) == true)
            {
                assignedType = new TypedArrayType(TypeHelper.CreateTypeUnion(allowedItemTypes), TypeSymbolValidationFlags.Default);
            }

            return(assignedType);
        }