コード例 #1
0
        private bool AddMethodToCache(SmallType pType, MethodSyntax pMethod, out MethodDefinition pDefinition)
        {
            //Check for duplicate method definitions
            Compiler.FindResult found;
            if (pMethod.SyntaxType == SyntaxType.Method)
            {
                found = _unit.MethodExists(pType, pMethod);
            }
            else if (pMethod.SyntaxType == SyntaxType.CastDefinition)
            {
                found = _unit.CastExists(pType, pMethod.Type, out MethodDefinition pDef);
            }
            else
            {
                throw new InvalidOperationException("Unknown method type " + pMethod.SyntaxType.ToString());
            }

            if (found != Compiler.FindResult.NotFound)
            {
                if (pMethod.SyntaxType == SyntaxType.Method)
                {
                    CompilerErrors.MethodDuplicate(pMethod, pMethod.Span);
                }
                else if (pMethod.SyntaxType == SyntaxType.CastDefinition)
                {
                    CompilerErrors.CastDuplicate(pMethod.Parameters[0].Type, pMethod.Type, pMethod.Span);
                }
                pDefinition = default;
                return(false);
            }
            else
            {
                //Create the tuple type if we are returning more than one value from a method
                //This will cache it in our SmallTypeCache so it can be found later
                if (pMethod.ReturnValues.Count > 1)
                {
                    SmallTypeCache.GetOrCreateTuple(SyntaxHelper.SelectNodeTypes(pMethod.ReturnValues));
                }

                //Set method and return types
                foreach (var p in pMethod.Parameters)
                {
                    _unit.FromString(p.TypeNode, out SmallType t);
                    p.TypeNode.SetType(t);
                }
                foreach (var r in pMethod.ReturnValues)
                {
                    _unit.FromString(r, out SmallType t);
                    r.SetType(t);
                }

                //Add method
                pDefinition = _unit.AddMethod(pType, pMethod);
                return(true);
            }
        }