// test ok
 void processEntiry(EntityDeclaration ed, TEntity te)
 {
     if (ed.HasModifier(Modifiers.Public))
     {
         te.isPublic = true;
     }
     if (ed.HasModifier(Modifiers.Private))
     {
         te.isPrivate = true;
     }
     if (ed.HasModifier(Modifiers.Internal))
     {
         te.isInternal = true;
     }
     if (ed.HasModifier(Modifiers.Override))
     {
         te.isOverride = true;
     }
     if (ed.HasModifier(Modifiers.Static))
     {
         te.isStatic = true;
     }
     te.name = ed.Name;
     te.type = ed.ReturnType.GetText();
     foreach (var attr in ed.Attributes)
     {
         te.attributes += attr.GetText();
     }
 }
Пример #2
0
 void CheckVirtual(EntityDeclaration entity)
 {
     if (!curType.Peek().HasModifier(Modifiers.Static) && !curType.Peek().HasModifier(Modifiers.Sealed) && entity.HasModifier(Modifiers.Virtual))
     {
         if (!entity.HasModifier(Modifiers.Public) && !entity.HasModifier(Modifiers.Protected) && !entity.HasModifier(Modifiers.Internal))
         {
             AddIssue(
                 entity.NameToken,
                 ctx.TranslateString("'virtual' members can't be private")
                 );
             return;
         }
     }
     if (!curType.Peek().HasModifier(Modifiers.Sealed) || !entity.HasModifier(Modifiers.Virtual))
     {
         return;
     }
     AddIssue(
         entity.ModifierTokens.First(t => t.Modifier == Modifiers.Virtual),
         ctx.TranslateString("'virtual' modifier is not usable in a sealed class"),
         ctx.TranslateString("Remove 'virtual' modifier"),
         s => {
         s.ChangeModifier(entity, entity.Modifiers & ~Modifiers.Virtual);
     }
         );
 }
Пример #3
0
            void CheckNode(EntityDeclaration node)
            {
                if (!node.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var type = node.Parent as TypeDeclaration;

                if (type == null || !type.HasModifier(Modifiers.Sealed))
                {
                    return;
                }
                foreach (var token_ in node.ModifierTokens)
                {
                    var token = token_;
                    if (token.Modifier == Modifiers.Sealed)
                    {
                        AddIssue(new CodeIssue(
                                     token,
                                     ctx.TranslateString("Keyword 'sealed' is redundant in sealed classes."),
                                     ctx.TranslateString("Remove redundant 'sealed' modifier"),
                                     script => script.ChangeModifier(node, node.Modifiers & ~Modifiers.Sealed)
                                     )
                        {
                            IssueMarker = IssueMarker.GrayOut
                        });
                    }
                }
            }
            void CheckStaticRequired(EntityDeclaration entity)
            {
                if (!curType.Peek().HasModifier(Modifiers.Static) || entity.HasModifier(Modifiers.Static) || entity.HasModifier(Modifiers.Const))
                {
                    return;
                }
                var fd = entity as FieldDeclaration;

                if (fd != null)
                {
                    foreach (var init in fd.Variables)
                    {
                        AddStaticRequiredError(entity, init.NameToken);
                    }
                    return;
                }

                var ed = entity as EventDeclaration;

                if (ed != null)
                {
                    foreach (var init in ed.Variables)
                    {
                        AddStaticRequiredError(entity, init.NameToken);
                    }
                    return;
                }

                AddStaticRequiredError(entity, entity.NameToken);
            }
Пример #5
0
        static bool GetAccessibility(EntityDeclaration element, out Accessibility acc)
        {
            if (element.Parent is TypeDeclaration && ((TypeDeclaration)element.Parent).ClassType == ClassType.Interface)
            {
                acc = Accessibility.Public;
                return(true);
            }
            bool result = false;

            acc = Accessibility.Private;
            if (element is TypeDeclaration && !(element.Parent is TypeDeclaration))
            {
                acc = Accessibility.Internal;
            }
            if (element.HasModifier(Modifiers.Public))
            {
                acc    = Accessibility.Public;
                result = true;
            }
            else if (element.HasModifier(Modifiers.Private))
            {
                acc    = Accessibility.Private;
                result = true;
            }
            else if (element.HasModifier(Modifiers.Protected | Modifiers.Internal))
            {
                acc    = Accessibility.ProtectedOrInternal;
                result = true;
            }
            else if (element.HasModifier(Modifiers.Protected))
            {
                acc    = Accessibility.Protected;
                result = true;
            }
            else if (element.HasModifier(Modifiers.Internal))
            {
                acc    = Accessibility.Internal;
                result = true;
            }
            return(result);
        }
Пример #6
0
            void Check(EntityDeclaration entity)
            {
                if (!entity.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var rr = ctx.Resolve(entity) as MemberResolveResult;

                if (rr == null || rr.IsError)
                {
                    return;
                }
                var method = rr.Member as IParameterizedMember;

                if (method == null)
                {
                    return;
                }
                var baseMethod = InheritanceHelper.GetBaseMember(method) as IParameterizedMember;

                if (baseMethod == null)
                {
                    return;
                }

                for (int i = 0; i < Math.Min(method.Parameters.Count, baseMethod.Parameters.Count); i++)
                {
                    var arg     = method.Parameters[i];
                    var baseArg = baseMethod.Parameters[i];

                    if (arg.Name != baseArg.Name)
                    {
                        int _i         = i;
                        var parameters = entity.GetChildrenByRole(Roles.Parameter);
                        AddIssue(new CodeIssue(
                                     parameters.ElementAt(_i).NameToken,
                                     ctx.TranslateString("Parameter name differs in base method declaration"),
                                     string.Format(ctx.TranslateString("Rename to '{0}'"), baseArg.Name),
                                     s => {
                            s.Rename(arg, baseArg.Name);
                        }
                                     ));
                    }
                }
            }
Пример #7
0
        protected virtual void EmitMethodParameters(IEnumerable <ParameterDeclaration> declarations, AstNode context, bool skipCloseParentheses = false)
        {
            this.WriteOpenParentheses();
            bool needComma = false;

            EntityDeclaration entityDeclaration = context as EntityDeclaration;

            if (entityDeclaration != null)
            {
                if (!entityDeclaration.HasModifier(Modifiers.Static))
                {
                    this.WriteThis();
                    if (declarations.Any())
                    {
                        this.WriteComma();
                    }
                }
            }

            foreach (ParameterDeclaration p in declarations)
            {
                string name = p.Name;
                name = name.Replace(Bridge.Translator.Emitter.FIX_ARGUMENT_NAME, "");
                if (this.Emitter.LocalsNamesMap != null && this.Emitter.LocalsNamesMap.ContainsKey(name))
                {
                    name = this.Emitter.LocalsNamesMap[name];
                }

                if (needComma)
                {
                    this.WriteComma();
                }

                needComma = true;
                this.Write(name);
            }

            if (!skipCloseParentheses)
            {
                this.WriteCloseParentheses();
            }
        }
Пример #8
0
        public JsFunctionDefinitionExpression CompileMethod(EntityDeclaration entity, BlockStatement body, IMethod method, MethodScriptSemantics impl)
        {
            bool             isIEnumerable = method.ReturnType.IsKnownType(KnownTypeCode.IEnumerable) || method.ReturnType.IsKnownType(KnownTypeCode.IEnumerableOfT);
            bool             isIEnumerator = method.ReturnType.IsKnownType(KnownTypeCode.IEnumerator) || method.ReturnType.IsKnownType(KnownTypeCode.IEnumeratorOfT);
            StateMachineType smt           = StateMachineType.NormalMethod;
            IType            iteratorBlockYieldTypeOrAsyncTaskGenericArgument = null;

            if ((isIEnumerable || isIEnumerator) && IsIteratorBlockVisitor.Analyze(body))
            {
                smt = isIEnumerable ? StateMachineType.IteratorBlockReturningIEnumerable : StateMachineType.IteratorBlockReturningIEnumerator;
                iteratorBlockYieldTypeOrAsyncTaskGenericArgument = method.ReturnType is ParameterizedType ? ((ParameterizedType)method.ReturnType).TypeArguments[0] : _compilation.FindType(KnownTypeCode.Object);
            }
            else if (entity.HasModifier(Modifiers.Async))
            {
                smt = (method.ReturnType.IsKnownType(KnownTypeCode.Void) ? StateMachineType.AsyncVoid : StateMachineType.AsyncTask);
                iteratorBlockYieldTypeOrAsyncTaskGenericArgument = method.ReturnType is ParameterizedType ? ((ParameterizedType)method.ReturnType).TypeArguments[0] : null;
            }

            CreateCompilationContext(entity, method, method.DeclaringTypeDefinition, (impl.Type == MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument ? _namer.ThisAlias : null));
            return(_statementCompiler.CompileMethod(method.Parameters, variables, body, impl.Type == MethodScriptSemantics.ImplType.StaticMethodWithThisAsFirstArgument, impl.ExpandParams, smt, iteratorBlockYieldTypeOrAsyncTaskGenericArgument));
        }
Пример #9
0
        public static void EmitMethodParameters(AbstractEmitterBlock block, IEnumerable <ParameterDeclaration> declarations, AstNode context)
        {
            bool needComma = false;

            EntityDeclaration entityDeclaration = context as EntityDeclaration;

            if (entityDeclaration != null)
            {
                if (!entityDeclaration.HasModifier(Modifiers.Static))
                {
                    block.WriteThis();
                    if (declarations.Any())
                    {
                        block.WriteComma();
                    }
                }
            }

            foreach (ParameterDeclaration p in declarations)
            {
                string name = p.Name;
                name = name.Replace(Bridge.Translator.Emitter.FIX_ARGUMENT_NAME, "");
                if (block.Emitter.LocalsNamesMap != null && block.Emitter.LocalsNamesMap.ContainsKey(name))
                {
                    name = block.Emitter.LocalsNamesMap[name];
                }

                if (needComma)
                {
                    block.WriteComma();
                }

                needComma = true;
                block.Write(name);
            }
        }
        public static string GetStockIcon(this EntityDeclaration element)
        {
            Accessibility acc = Accessibility.None;

            // type accessibility
            acc = Accessibility.Internal;
            if (element.HasModifier(Modifiers.Public))
            {
                acc = Accessibility.Public;
            }
            else if (element.HasModifier(Modifiers.Protected))
            {
                acc = Accessibility.Protected;
            }
            else if (element.HasModifier(Modifiers.Private))
            {
                acc = Accessibility.Private;
            }

            if (element is TypeDeclaration)
            {
                var type = element as TypeDeclaration;
                switch (type.ClassType)
                {
                case ClassType.Class:
                    return(typeIconTable [0, ModifierToOffset(acc)]);

                case ClassType.Struct:
                    return(typeIconTable [3, ModifierToOffset(acc)]);

                case ClassType.Interface:
                    return(typeIconTable [2, ModifierToOffset(acc)]);

                case ClassType.Enum:
                    return(typeIconTable [1, ModifierToOffset(acc)]);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            if (element is DelegateDeclaration)
            {
                return(typeIconTable [4, ModifierToOffset(acc)]);
            }

            // member accessibility
            acc = Accessibility.Private;
            if (element.HasModifier(Modifiers.Public))
            {
                acc = Accessibility.Public;
            }
            else if (element.HasModifier(Modifiers.Protected))
            {
                acc = Accessibility.Protected;
            }
            else if (element.HasModifier(Modifiers.Internal))
            {
                acc = Accessibility.Internal;
            }

            if (element is MethodDeclaration)
            {
                var method = element as MethodDeclaration;
                if (method.IsExtensionMethod)
                {
                    return(extensionMethodIconTable [ModifierToOffset(acc)]);
                }
                return(methodIconTable [ModifierToOffset(acc)]);
            }
            if (element is OperatorDeclaration || element is ConstructorDeclaration || element is DestructorDeclaration || element is Accessor)
            {
                return(methodIconTable [ModifierToOffset(acc)]);
            }

            if (element is PropertyDeclaration)
            {
                return(propertyIconTable [ModifierToOffset(acc)]);
            }
            if (element is EventDeclaration || element is CustomEventDeclaration)
            {
                return(eventIconTable [ModifierToOffset(acc)]);
            }

            return(fieldIconTable [ModifierToOffset(acc)]);
        }