Example #1
0
        public static TypeSyntax GetTypeNameSyntax(this TypeMember type)
        {
            var clrBinding = type.Bindings.FirstOrDefault <System.Type>();

            if (clrBinding != null)
            {
                if (clrBinding.IsArray)
                {
                    var elementTypeSyntax = GetTypeNameSyntax(type.UnderlyingType);
                    return(SyntaxFactory.ArrayType(elementTypeSyntax)
                           .WithRankSpecifiers(SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression())))));
                }

                if (_s_keywordPerType.TryGetValue(clrBinding, out SyntaxKind keyword))
                {
                    return(SyntaxFactory.PredefinedType(SyntaxFactory.Token(keyword)));
                }

                if (IsNullableValueType(clrBinding.GetTypeInfo(), out Type underlyingValueType))
                {
                    var context = CodeGeneratorContext.GetContextOrThrow();
                    return(SyntaxFactory.NullableType(GetTypeNameSyntax(context.FindType(underlyingValueType))));
                }
            }

            return(GetTypeFullNameSyntax(type));
        }
Example #2
0
        private void FlushImportDirective()
        {
            var context = CodeGeneratorContext.GetContextOrThrow();
            var module  = context.LookupStateOrThrow <ModuleMember>();

            module.Imports.Add(Directive);
        }
Example #3
0
        protected void GenerateEventHandlerActionKey(IdentifierName eventName, IReadOnlyList <IFunctionContext> handlerList)
        {
            KEY(GetEventActionKeyName(eventName), LAMBDA(@model
                                                         => DO.RETURN(ASYNC.LAMBDA((@state, @actions) => {
                LOCAL("newModel", out var @newModel, USE("Object").DOT("assign").INVOKE(INITOBJECT(), @model));

                var modelMemberAccessRewriter = new ModelMemberAccessRewriter(Metadata.Page, @newModel);
                var apiMemberAccessRewriter   = new BackendApiProxyAccessRewriter(Metadata.Page);

                var actionBlock = CodeGeneratorContext.GetContextOrThrow().GetCurrentBlock();

                foreach (var handler in handlerList)
                {
                    var rewrittenHandlerBody =
                        apiMemberAccessRewriter.RewriteBlockStatement(
                            modelMemberAccessRewriter.RewriteBlockStatement(handler.Body));

                    foreach (var statement in rewrittenHandlerBody.Statements)
                    {
                        actionBlock.AppendStatement(statement);
                    }
                }

                actions.DOT("replaceModel").INVOKE(@newModel);
            }))
                                                         ));
        }
Example #4
0
        public void ELSE(Action body)
        {
            _statement.ElseBlock = new BlockStatement();

            using (CodeGeneratorContext.GetContextOrThrow().PushState(_statement.ElseBlock))
            {
                body?.Invoke();
            }
        }
Example #5
0
        public FluentElse THEN(Action body)
        {
            _statement.ThenBlock = new BlockStatement();

            var context = CodeGeneratorContext.GetContextOrThrow();

            using (context.PushState(new BlockContext(_statement.ThenBlock)))
            {
                body?.Invoke();
            }

            return(new FluentElse(_statement));
        }
Example #6
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static bool IsTypeNamespaceImported(TypeMember type)
        {
            var importContext = CodeGeneratorContext.GetContextOrThrow().LookupStateOrThrow <ImportContext>();

            if (importContext.IsTypeImported(type))
            {
                return(true);
            }

            if (type.IsGenericType && type.GenericTypeDefinition != null)
            {
                return(importContext.IsTypeImported(type.GenericTypeDefinition));
            }

            return(false);
        }
Example #7
0
        public SyntaxTree EmitSyntax()
        {
            DetermineNamespaceImports(GetReferencedTypes(), out var usingSyntaxList, out var importContext);

            using (CodeGeneratorContext.GetContextOrThrow().PushState(importContext))
            {
                var typeSyntax = TypeSyntaxEmitter.GetSyntax(_type);
                var unitSyntax = SyntaxFactory.CompilationUnit()
                                 .WithUsings(SyntaxFactory.List(usingSyntaxList))
                                 .WithMembers(
                    SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                        SyntaxFactory.NamespaceDeclaration(CreateQualifiedNameSyntax(_type.Namespace.Split('.')))
                        .WithMembers(SyntaxFactory.SingletonList(typeSyntax))))
                                 .NormalizeWhitespace();

                return(unitSyntax.SyntaxTree);
            }
        }
Example #8
0
        public FluentMember(bool?isAsync = null, bool?isReadOnly = null, bool?isDefaultExport = null)
        {
            var traits = CodeGeneratorContext.GetContextOrThrow().PeekStateOrThrow <MemberTraitsContext>();

            if (isAsync.HasValue)
            {
                traits.IsAsync = isAsync.Value;
            }

            if (isReadOnly.HasValue)
            {
                traits.IsReadOnly = isReadOnly.Value;
            }

            if (isDefaultExport.HasValue)
            {
                traits.IsDefaultExport = isDefaultExport.Value;
            }
        }
Example #9
0
        public static TypeMember BuildTypeMember(TypeMemberKind typeKind, IdentifierName name, Action body)
        {
            var context          = CodeGeneratorContext.GetContextOrThrow();
            var module           = context.TryLookupState <ModuleMember>();
            var namespaceContext = context.TryLookupState <NamespaceContext>();
            var traits           = context.PopStateOrThrow <MemberTraitsContext>();
            var containingType   = context.TryLookupState <TypeMember>();

            var type = new TypeMember();

            type.Name            = name;
            type.Namespace       = namespaceContext?.Name;
            type.TypeKind        = typeKind;
            type.DeclaringModule = module;
            type.DeclaringType   = containingType;
            type.Modifier        = traits.Modifier;
            type.Visibility      = traits.Visibility;
            type.IsDefaultExport = traits.IsDefaultExport;

            if (containingType != null)
            {
                containingType.Members.Add(type);
            }

            context.AddGeneratedMember(type, isTopLevel: containingType == null);
            if (module != null)
            {
                module.Members.Add(type);
            }

            using (context.PushState(type))
            {
                body?.Invoke();
            }

            return(type);
        }
Example #10
0
 public FluentModifier(MemberModifier modifier)
 {
     CodeGeneratorContext.GetContextOrThrow().PeekStateOrThrow <MemberTraitsContext>().Modifier = modifier;
 }
Example #11
0
 public FluentVisibility(MemberVisibility visibility)
 {
     CodeGeneratorContext.GetContextOrThrow().PushState(new MemberTraitsContext(visibility));
 }
Example #12
0
 public static BlockContext GetBlockOrThrow()
 {
     return(CodeGeneratorContext.GetContextOrThrow().GetCurrentBlock());
 }
Example #13
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public static ExpressionSyntax GetLiteralSyntax(object value)
        {
            if (value is ConstantExpression expression)
            {
                value = expression.Value;
            }
            if (value == null)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
            }
            else if (value is int intValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(intValue)));
            }
            else if (value is float floatValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(floatValue)));
            }
            else if (value is long longValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(longValue)));
            }
            else if (value is decimal decimalValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(decimalValue)));
            }
            else if (value is uint uintValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(uintValue)));
            }
            else if (value is double doubleValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(doubleValue)));
            }
            else if (value is ulong ulongValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(ulongValue)));
            }
            else if (value is string stringValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(stringValue)));
            }
            else if (value is char charValue)
            {
                return(SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal(charValue)));
            }
            else if (value is bool boolValue)
            {
                return(SyntaxFactory.LiteralExpression(boolValue ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression));
            }
            else if (value is Type typeValue)
            {
                var context = CodeGeneratorContext.GetContextOrThrow();
                return(SyntaxFactory.TypeOfExpression(GetTypeNameSyntax(context.FindType(typeValue))));
            }
            else if (value is TypeMember typeMember)
            {
                return(SyntaxFactory.TypeOfExpression(GetTypeNameSyntax(typeMember)));
            }

            throw new NotSupportedException($"Literals of type {value.GetType().Name} are not supported");
        }