Exemplo n.º 1
0
        public void Clone(AstFunctionReference functionRef,
                          AstFunctionDefinition templateFunctionDef, AstTemplateInstanceFunction instanceFunction,
                          AstTemplateArgumentMap argumentMap)
        {
            if (templateFunctionDef is AstFunctionDefinition functionDef)
            {
                _argumentMap = argumentMap;
                _current.SetCurrent(instanceFunction.FunctionType);
                _current.SetCurrent(instanceFunction);
                functionDef.VisitChildren(this);
                _current.RevertCurrent();
                _current.RevertCurrent();

                instanceFunction.SetIdentifier(functionRef.Identifier);

                AstSymbolTable symbols;
                if (templateFunctionDef is IAstSymbolTableSite symbolSite)
                {
                    // registered in impl function defs symbol table
                    symbols = symbolSite.SymbolTable;
                }
                else
                {
                    // intrinsics are registered in root symbol table
                    symbols = functionRef.Symbol.SymbolTable.GetRootTable();
                }

                instanceFunction.CreateSymbols(symbols);
            }
            else
            {
                throw new NotImplementedException(
                          "[Interop] .NET Generics not implemented yet.");
            }
        }
        public void Instantiate(CompilerContext context, AstFunctionReference function)
        {
            Context            = function.Context;
            _templateArguments = new AstTemplateArgumentMap(
                TemplateDefinition.TemplateParameters, function.TemplateArguments);

            var cloner = new AstNodeCloner(context, TemplateDefinition.Indent);

            cloner.Clone(function, TemplateDefinition, this, _templateArguments);
        }
Exemplo n.º 3
0
        private AstFunctionReference CloneFunctionReference(AstFunctionReference function)
        {
            Ast.Guard(function.Context, "No FunctionReference Context set.");
            var fnRef = new AstFunctionReference(function.Context !, function.EnforceReturnValueUse);

            var typeArg = _argumentMap !.LookupArgument(function.Identifier);

            // a conversion function may be templated by a type (T)
            if (typeArg is null || !typeArg.HasTypeReference)
            {
                fnRef.SetIdentifier(function.Identifier);
            }
Exemplo n.º 4
0
        public static AstFunctionDefinition?ResolveOverloads(AstFunctionReference function)
        {
            if (function?.Symbol is null)
            {
                throw new InternalErrorException("No Symbol is set on the Function Reference.");
            }

            var symbol = function.Symbol.DefinitionSymbol !;

            return(symbol.FunctionOverloads
                   .Select(functionDef => new RankInfo <AstFunctionDefinition>
            {
                Object = functionDef, Rank = Rank(function, functionDef)
            })
                   .Where(rank => rank.Rank >= 0)
                   .OrderByDescending(rank => rank.Rank)
                   .Select(r => r.Object)
                   .FirstOrDefault());
        }
Exemplo n.º 5
0
        public static int Rank(AstFunctionReference functionRef, AstFunctionDefinition functionDef)
        {
            var sources = functionRef.FunctionType.Arguments.Select(p => p.TypeReference).ToList();
            var targets = functionDef.FunctionType.Parameters.Select(p => p.TypeReference).ToList();

            // TODO: check for default values on targets
            if (sources.Count != targets.Count)
            {
                return(0);
            }

            if (functionRef.FunctionType.HasTypeReference &&
                functionDef.FunctionType.HasTypeReference)
            {
                sources.Add(functionRef.FunctionType.TypeReference);
                targets.Add(functionDef.FunctionType.TypeReference);
            }

            return(Rank(sources, targets));
        }
Exemplo n.º 6
0
 public override void VisitFunctionReference(AstFunctionReference function)
 => CloneFunctionReference(function);
Exemplo n.º 7
0
 public static AstMessage FunctionReturnValueNotUsed(this AstErrorSite errorSite, AstFunctionReference function)
 {
     return(errorSite.AddError(function, function.Context !,
                               $"The return value must be assigned for Function '{function}'"));
 }
Exemplo n.º 8
0
 public static AstMessage UndefinedFunction(this AstErrorSite errorSite, AstFunctionReference function)
 {
     return(errorSite.AddError(function, function.Context !,
                               $"Reference to an undefined Function '{function}'"));
 }