Exemplo n.º 1
0
 private static void RemoveTypeHint(ModuleBodyElementDeclaration member, ParserRuleContext functionContext, IModuleRewriter rewriter)
 {
     if (member.TypeHint != null)
     {
         rewriter.Remove(functionContext.GetDescendent <VBAParser.TypeHintContext>());
     }
 }
Exemplo n.º 2
0
 private void RemoveReturnStatements(ModuleBodyElementDeclaration member, IModuleRewriter rewriter)
 {
     foreach (var returnStatement in GetReturnStatements(member))
     {
         rewriter.Remove(returnStatement);
     }
 }
Exemplo n.º 3
0
        private void UpdateSignature(Declaration targetVariable, ModuleBodyElementDeclaration enclosingMember, IRewriteSession rewriteSession)
        {
            var paramList = enclosingMember.Context.GetChild <VBAParser.ArgListContext>();

            if (enclosingMember.DeclarationType.HasFlag(DeclarationType.Property))
            {
                UpdateProperties(enclosingMember, targetVariable, rewriteSession);
            }
            else
            {
                AddParameter(enclosingMember, targetVariable, paramList, rewriteSession);
            }

            var interfaceImplementation = enclosingMember.InterfaceMemberImplemented;

            if (interfaceImplementation == null)
            {
                return;
            }

            UpdateSignature(interfaceImplementation, targetVariable, rewriteSession);

            var interfaceImplementations = _declarationFinderProvider.DeclarationFinder.FindInterfaceImplementationMembers(enclosingMember.InterfaceMemberImplemented)
                                           .Where(member => !ReferenceEquals(member, enclosingMember));

            foreach (var implementation in interfaceImplementations)
            {
                UpdateSignature(implementation, targetVariable, rewriteSession);
            }
        }
Exemplo n.º 4
0
 public InterfaceMember(Declaration member, ICodeBuilder codeBuilder)
 {
     Member = member;
     if (!(member is ModuleBodyElementDeclaration mbed))
     {
         throw new ArgumentException();
     }
     _element     = mbed;
     _codeBuilder = codeBuilder;
 }
Exemplo n.º 5
0
        private void ConvertPropertyGet(ModuleBodyElementDeclaration member, VBAParser.PropertyGetStmtContext propertyGetContext, IModuleRewriter rewriter)
        {
            RemoveAsTypeDeclaration(propertyGetContext, rewriter);
            RemoveTypeHint(member, propertyGetContext, rewriter);

            ConvertPropertyGetDeclaration(propertyGetContext, rewriter);
            ConvertExitPropertyStatements(propertyGetContext, rewriter);

            RemoveReturnStatements(member, rewriter);
        }
Exemplo n.º 6
0
        private void ConvertFunction(ModuleBodyElementDeclaration member, VBAParser.FunctionStmtContext functionContext, IModuleRewriter rewriter)
        {
            RemoveAsTypeDeclaration(functionContext, rewriter);
            RemoveTypeHint(member, functionContext, rewriter);

            ConvertFunctionDeclaration(functionContext, rewriter);
            ConvertExitFunctionStatements(functionContext, rewriter);

            RemoveReturnStatements(member, rewriter);
        }
Exemplo n.º 7
0
        private bool IsIssueItself(ModuleBodyElementDeclaration declaration)
        {
            var procedureCallReferences = ProcedureCallReferences(declaration).ToHashSet();

            if (!procedureCallReferences.Any())
            {
                return(false);
            }

            return(declaration.References
                   .All(reference => procedureCallReferences.Contains(reference) ||
                        reference.IsAssignment && IsReturnStatement(declaration, reference)));
        }
Exemplo n.º 8
0
        private static void AddByRefIdentifierToImplementations(
            ModuleBodyElementDeclaration interfaceMember,
            int parameterIndex,
            DeclarationFinder finder,
            IRewriteSession rewriteSession)
        {
            var implementationParameters = finder.FindInterfaceImplementationMembers(interfaceMember)
                                           .Select(implementation => implementation.Parameters[parameterIndex]);

            foreach (var parameter in implementationParameters)
            {
                AddByRefIdentifier(rewriteSession, parameter);
            }
        }
Exemplo n.º 9
0
        private bool IsInterfaceIssue(ModuleBodyElementDeclaration declaration, DeclarationFinder finder)
        {
            if (!IsIssueItself(declaration))
            {
                return(false);
            }

            var implementations = finder.FindInterfaceImplementationMembers(declaration);

            return(implementations.All(implementation => IsIssueItself(implementation) ||
                                       implementation.References.All(reference =>
                                                                     reference.IsAssignment &&
                                                                     IsReturnStatement(implementation, reference))));
        }
Exemplo n.º 10
0
        private void ConvertMember(ModuleBodyElementDeclaration member, IRewriteSession rewriteSession)
        {
            var rewriter = rewriteSession.CheckOutModuleRewriter(member.QualifiedModuleName);

            switch (member.Context)
            {
            case VBAParser.FunctionStmtContext functionContext:
                ConvertFunction(member, functionContext, rewriter);
                break;

            case VBAParser.PropertyGetStmtContext propertyGetContext:
                ConvertPropertyGet(member, propertyGetContext, rewriter);
                break;
            }
        }
Exemplo n.º 11
0
        public string ImprovedArgumentList(ModuleBodyElementDeclaration declaration)
        {
            var arguments = Enumerable.Empty <string>();

            if (declaration is IParameterizedDeclaration parameterizedDeclaration)
            {
                arguments = parameterizedDeclaration.Parameters
                            .OrderBy(parameter => parameter.Selection)
                            .Select(parameter => BuildParameterDeclaration(
                                        parameter,
                                        parameter.Equals(parameterizedDeclaration.Parameters.LastOrDefault()) &&
                                        declaration.DeclarationType.HasFlag(DeclarationType.Property) &&
                                        !declaration.DeclarationType.Equals(DeclarationType.PropertyGet)));
            }
            return($"{string.Join(", ", arguments)}");
        }
Exemplo n.º 12
0
        public string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
                                                    string content       = null,
                                                    string accessibility = null,
                                                    string newIdentifier = null)
        {
            var elements = new List <string>()
            {
                ImprovedFullMemberSignatureInternal(declaration, accessibility, newIdentifier),
                Environment.NewLine,
                string.IsNullOrEmpty(content) ? null : $"{content}{Environment.NewLine}",
                ProcedureEndStatement(declaration.DeclarationType),
                Environment.NewLine,
            };

            return(string.Concat(elements));
        }
Exemplo n.º 13
0
        private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration declaration, string accessibility = null, string newIdentifier = null)
        {
            var accessibilityToken = declaration.Accessibility.Equals(Accessibility.Implicit)
                                                    ? Tokens.Public
                                                    : $"{declaration.Accessibility.ToString()}";

            var asTypeName = string.IsNullOrEmpty(declaration.AsTypeName)
                                                            ? string.Empty
                                                            : $" {Tokens.As} {declaration.AsTypeName}";

            var elements = new List <string>()
            {
                accessibility ?? accessibilityToken,
                $" {ProcedureTypeStatement(declaration.DeclarationType)} ",
                newIdentifier ?? declaration.IdentifierName,
                $"({ImprovedArgumentList(declaration)})",
                asTypeName
            };

            return(string.Concat(elements).Trim());
        }
 /// <summary>
 /// Removes a member declaration and subsequent <c>VBAParser.EndOfStatementContext</c>
 /// depending on the <paramref name="removeEndOfStmtContext"/> flag.
 /// </summary>
 /// <remarks>
 /// Calling this function with <paramref name="removeEndOfStmtContext"/> defaulted to <c>true</c>
 /// avoids leaving residual newlines between the deleted declaration and the next declaration.
 /// </remarks>
 public static void RemoveMember(this IModuleRewriter rewriter, ModuleBodyElementDeclaration target, bool removeEndOfStmtContext = true)
 {
     RemoveMembers(rewriter, new ModuleBodyElementDeclaration[] { target }, removeEndOfStmtContext);
 }
Exemplo n.º 15
0
 private static string MemberBlockFromPrototypeTest(ModuleBodyElementDeclaration mbed, MemberBlockFromPrototypeTestParams testParams)
 => CreateCodeBuilder().BuildMemberBlockFromPrototype(mbed, testParams.Content, testParams.Accessibility, testParams.NewIdentifier);
Exemplo n.º 16
0
 public MemberBlockFromPrototypeTestParams(ModuleBodyElementDeclaration mbed, Accessibility accessibility = Accessibility.Public, string content = null, string newIdentifier = null)
 {
     Accessibility = accessibility;
     Content       = content;
     NewIdentifier = newIdentifier;
 }
Exemplo n.º 17
0
 private static bool MethodIsResult(ModuleBodyElementDeclaration member)
 {
     return(!member.IsInterfaceImplementation);
 }
Exemplo n.º 18
0
 public IntroduceParameterModel(Declaration target, ModuleBodyElementDeclaration enclosingMember)
 {
     Target          = target;
     EnclosingMember = enclosingMember;
 }
Exemplo n.º 19
0
 public string ImprovedFullMemberSignature(ModuleBodyElementDeclaration declaration)
 => ImprovedFullMemberSignatureInternal(declaration);
Exemplo n.º 20
0
        private static bool AllImplementationParametersCanBeChangedToByVal(ParameterDeclaration parameter, ModuleBodyElementDeclaration interfaceMember, DeclarationFinder finder)
        {
            if (!TryFindParameterIndex(parameter, interfaceMember, out var parameterIndex))
            {
                //This really should never happen.
                Debug.Fail($"Could not find index for parameter {parameter.IdentifierName} in interface member {interfaceMember.IdentifierName}.");
                return(false);
            }

            var implementations = finder.FindInterfaceImplementationMembers(interfaceMember);

            return(implementations.All(implementation => ParameterAtIndexCanBeChangedToBePassedByValIfRelatedParameterCan(implementation, parameterIndex, finder)));
        }
        private static bool ThereAreImplementationsAndNoneUsesTheParameter(ParameterDeclaration parameter, ModuleBodyElementDeclaration interfaceMember, DeclarationFinder finder)
        {
            if (!TryFindParameterIndex(parameter, interfaceMember, out var parameterIndex))
            {
                //This really should never happen.
                Debug.Fail($"Could not find index for parameter {parameter.IdentifierName} in interface member {interfaceMember.IdentifierName}.");
                return(false);
            }

            var implementations = finder.FindInterfaceImplementationMembers(interfaceMember).ToList();

            //We do not want to report all parameters of not implemented interfaces.
            return(implementations.Any() &&
                   implementations.All(implementation => ParameterAtIndexIsNotUsed(implementation, parameterIndex)));
        }
Exemplo n.º 22
0
 private static string ImprovedArgumentListTest(ModuleBodyElementDeclaration mbed)
 => CreateCodeBuilder().ImprovedArgumentList(mbed);
 private static IEnumerable <QualifiedSelection> SingleVariableArgumentSelections(ModuleBodyElementDeclaration member)
 {
     return(member.Parameters
            .SelectMany(parameter => parameter.ArgumentReferences)
            .Select(SingleVariableArgumentSelection)
            .Where(maybeSelection => maybeSelection.HasValue)
            .Select(selection => selection.Value));
 }
 private static bool IsPropertyMutatorRHSParameter(ModuleBodyElementDeclaration enclosingMethod, ParameterDeclaration implicitByRefParameter)
 {
     return((enclosingMethod.DeclarationType.HasFlag(DeclarationType.PropertyLet) ||
             enclosingMethod.DeclarationType.HasFlag(DeclarationType.PropertySet)) &&
            enclosingMethod.Parameters.Last().Equals(implicitByRefParameter));
 }