protected override void Visit(CodeMethod method)
        {
            var isOverload = false;

            if (_overloads !.TryGetValue(method.Name.Name, out var overloads))
            {
                // here we test if new method could be used as overload alongside with other methods with such name
                // and if it cannot be used as overload we should change it's name to remove naming conflict
                //
                // overload logic
                // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#signatures-and-overloading
                // overload key contains:
                // 1. name (used for _overloads dictionary key)
                // 2. number of type parameters (skipped, as currently missing from our code model)
                // 3. for each parameter (positional): parameter type (without NRT annotation) + direction (kind)
                // NOTE: ref direction is same as out direction for overload resolution logic
                isOverload = true;
                foreach (var overload in overloads)
                {
                    // TODO: add type parameters count check when added to code model
                    if (overload.Parameters.Count == method.Parameters.Count)
                    {
                        var sameParameters = true;
                        for (var i = 0; i < overload.Parameters.Count; i++)
                        {
                            if (overload.Parameters[i].Direction != method.Parameters[i].Direction &&
                                (overload.Parameters[i].Direction == CodeParameterDirection.In ||
                                 method.Parameters[i].Direction == CodeParameterDirection.In))
                            {
                                sameParameters = false;
                                break;
                            }

                            if (!_languageProvider.TypeEqualityComparerWithoutNRT.Equals(overload.Parameters[i].Type !.Type, method.Parameters[i].Type !.Type))
                            {
                                sameParameters = false;
                                break;
                            }
                        }

                        // found overload with same set of parameters (according to overload resolution procedure)
                        // so we cannot use this method as overload for current method group
                        if (sameParameters)
                        {
                            isOverload = false;
                            break;
                        }
                    }
                }

                if (isOverload)
                {
                    overloads.Add(method);
                }
            }

            if (!isOverload)
            {
                FixName(_memberNames !, method.Name, null, true);

                // create new method group for current method
                // note that we do not try to find another existing overloads group with name derived from
                // initial method name as it doesn't make any sense to create new artifical group of overloads
                _overloads.Add(method.Name.Name, new List <CodeMethod>()
                {
                    method
                });
            }

            // as we don't have local methods yet, we don't need to save/reuse method contexts
            _parametersAndVariables = CreateNewNamesSet();
            base.Visit(method);
            _parametersAndVariables = null;
        }
Exemple #2
0
 protected override void Visit(CodeMethod method)
 {
     AddNameToCurrentScope(method.Name, false);
     base.Visit(method);
 }