Exemplo n.º 1
0
        public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
        => AssignTypeWithDiagnostics(syntax, diagnostics => {
            var stringSyntax = syntax.TryGetType();

            if (stringSyntax != null && stringSyntax.IsInterpolated())
            {
                // TODO: in the future, we can relax this check to allow interpolation with compile-time constants.
                // right now, codegen will still generate a format string however, which will cause problems for the type.
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).ResourceTypeInterpolationUnsupported()));
            }

            var stringContent = stringSyntax?.TryGetLiteralValue();
            if (stringContent == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            // TODO: This needs proper namespace, type, and version resolution logic in the future
            var typeReference = ResourceTypeReference.TryParse(stringContent);
            if (typeReference == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidResourceType()));
            }

            var declaredType = resourceTypeRegistrar.GetType(typeReference);

            return(TypeValidator.NarrowTypeAndCollectDiagnostics(typeManager, syntax.Body, declaredType, diagnostics));
        });
Exemplo n.º 2
0
        public override void VisitParameterDeclarationSyntax(ParameterDeclarationSyntax syntax)
        => AssignTypeWithDiagnostics(syntax, diagnostics => {
            diagnostics.AddRange(this.ValidateIdentifierAccess(syntax));

            // assume "any" type when the parameter has parse errors (either missing or was skipped)
            var declaredType = syntax.ParameterType == null
                    ? LanguageConstants.Any
                    : LanguageConstants.TryGetDeclarationType(syntax.ParameterType.TypeName);

            if (declaredType == null)
            {
                return(new ErrorTypeSymbol(DiagnosticBuilder.ForPosition(syntax.Type).InvalidParameterType()));
            }

            var assignedType = declaredType;
            if (object.ReferenceEquals(assignedType, LanguageConstants.String))
            {
                var allowedItemTypes = SyntaxHelper.TryGetAllowedItems(syntax)?
                                       .Select(item => typeManager.GetTypeInfo(item));

                if (allowedItemTypes != null && allowedItemTypes.All(itemType => itemType is StringLiteralType))
                {
                    assignedType = UnionType.Create(allowedItemTypes);
                }
            }

            switch (syntax.Modifier)
            {
            case ParameterDefaultValueSyntax defaultValueSyntax:
                diagnostics.AddRange(ValidateDefaultValue(defaultValueSyntax, assignedType));
                break;

            case ObjectSyntax modifierSyntax:
                var modifierType = LanguageConstants.CreateParameterModifierType(declaredType, assignedType);
                // we don't need to actually use the narrowed type; just need to use this to collect assignment diagnostics
                TypeValidator.NarrowTypeAndCollectDiagnostics(typeManager, modifierSyntax, modifierType, diagnostics);
                break;
            }

            return(assignedType);
        });