Exemple #1
0
        static void DeclareArguments(IFunctionDeclaration function, NamedCollection <IArgumentDeclaration> arguments, ICppScope scope, string kind)
        {
            if (arguments.IsEmpty())
            {
                return;
            }
            var typeName = GetFunctionTypeName(function.Name, kind);

            scope.Declaration.AddLine($"struct {typeName} {{");
            scope.WithDeclarationIndented(
                innerScope => {
                foreach (var argument in arguments)
                {
                    var argTypeName = GetArgumentTypeName(argument.Type);
                    var argName     = CppEscape(argument.Name);
                    if (argument.IsAssignable && kind != "Result")
                    {
                        innerScope.Declaration.AddLine($"{argTypeName}* {argName};");
                        if (arguments.Count == 1)
                        {
                            innerScope.Declaration.AddLine($"operator {argTypeName}() const {{ return *{argName}; }}");
                        }
                    }
                    else
                    {
                        innerScope.Declaration.AddLine($"{argTypeName} {argName};");
                        if (arguments.Count == 1)
                        {
                            innerScope.Declaration.AddLine($"operator {argTypeName}() const {{ return {argName}; }}");
                        }
                    }
                }
            });
            scope.Declaration.AddLine(line: "};");
        }
Exemple #2
0
        static void DeclareFunctionBody(IFunctionDeclaration function, ICppScope scope)
        {
            var noResult = function.Results.IsEmpty();
            var noLeft   = function.LeftArguments.IsEmpty();
            var noRight  = function.RightArguments.IsEmpty();

            var resultType = noResult ? "void" : GetFunctionTypeName(function.Name, kind: "Result");
            var left       = string.Empty;
            var leftLocal  = string.Empty;
            var right      = string.Empty;
            var rightLocal = string.Empty;

            if (!noLeft)
            {
                leftLocal = scope.MakeLocalName(hint: "left");
                left      = GetFunctionTypeName(function.Name, kind: "Left") + " " + leftLocal;
            }
            if (!noRight)
            {
                rightLocal = scope.MakeLocalName(hint: "right");
                right      = (noLeft ? "" : ", ") + GetFunctionTypeName(function.Name, kind: "Right") + " " + rightLocal;
            }

            scope.Declaration.AddLine($"inline {resultType} {CppEscape(function.Name)}({left}{right}) {{");
            scope.WithDeclarationIndented(
                innerScope => {
                MakeArgumentLocals(function.LeftArguments, leftLocal, innerScope);
                MakeArgumentLocals(function.RightArguments, rightLocal, innerScope);
                MakeResultLocals(function.Results, innerScope);

                Dynamic(function.Implementation, innerScope);

                if (!noResult)
                {
                    var resultLocal = scope.MakeLocalName(hint: "result");
                    innerScope.Runtime.AddLine($"{resultType} {resultLocal};");
                    AssignResultsFromLocals(resultLocal, function.Results, innerScope);
                    innerScope.Runtime.AddLine($"return {resultLocal};");
                }
            });
            scope.Declaration.AddLine(line: "}");
        }