private static void RemoveTypeHint(ModuleBodyElementDeclaration member, ParserRuleContext functionContext, IModuleRewriter rewriter) { if (member.TypeHint != null) { rewriter.Remove(functionContext.GetDescendent <VBAParser.TypeHintContext>()); } }
private void RemoveReturnStatements(ModuleBodyElementDeclaration member, IModuleRewriter rewriter) { foreach (var returnStatement in GetReturnStatements(member)) { rewriter.Remove(returnStatement); } }
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); } }
public InterfaceMember(Declaration member, ICodeBuilder codeBuilder) { Member = member; if (!(member is ModuleBodyElementDeclaration mbed)) { throw new ArgumentException(); } _element = mbed; _codeBuilder = codeBuilder; }
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); }
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); }
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))); }
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); } }
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)))); }
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; } }
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)}"); }
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)); }
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); }
private static string MemberBlockFromPrototypeTest(ModuleBodyElementDeclaration mbed, MemberBlockFromPrototypeTestParams testParams) => CreateCodeBuilder().BuildMemberBlockFromPrototype(mbed, testParams.Content, testParams.Accessibility, testParams.NewIdentifier);
public MemberBlockFromPrototypeTestParams(ModuleBodyElementDeclaration mbed, Accessibility accessibility = Accessibility.Public, string content = null, string newIdentifier = null) { Accessibility = accessibility; Content = content; NewIdentifier = newIdentifier; }
private static bool MethodIsResult(ModuleBodyElementDeclaration member) { return(!member.IsInterfaceImplementation); }
public IntroduceParameterModel(Declaration target, ModuleBodyElementDeclaration enclosingMember) { Target = target; EnclosingMember = enclosingMember; }
public string ImprovedFullMemberSignature(ModuleBodyElementDeclaration declaration) => ImprovedFullMemberSignatureInternal(declaration);
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))); }
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)); }