Ejemplo n.º 1
0
        public virtual bool CanIdDeclared(Identifier Id)
        {
            var StructuredContainer = Id.Container.RealContainer as StructuredScope;
            var IsLocal             = Id.Container.FunctionScope != null;

            if (Id.Access == IdentifierAccess.Unknown)
            {
                if (!IsLocal)
                {
                    throw new ApplicationException("Only locals' access can be unknown");
                }
            }
            else
            {
                if (IsLocal)
                {
                    throw new ApplicationException("Locals' access must be unknown");
                }

                if (((int)Id.Access & 3) != 0 && StructuredContainer == null)
                {
                    State.Messages.Add(MessageId.ProtPrivInNonStructured, Id.Declaration);
                    return(false);
                }
            }

            /*
             *          if (Id.Name.IsValid && !Id.Name.IsValidIdentifierName)
             *          {
             *                  State.Messages.Add(MessageId.NotValidName, Id.Name);
             *                  return false;
             *          }
             */

            if (Id is Namespace)
            {
                if (!(this is NamespaceScope))
                {
                    throw new ApplicationException("Namespaceses cannot be declared here");
                }
            }
            else if (Id is Property)
            {
                var Property  = Id as Property;
                var PropScope = Property.PropertyScope;
                var Type      = Id.Children[0];

                if (Identifiers.IsLessAccessable(Type, Id))
                {
                    State.Messages.Add(MessageId.LessAccessable, Id.Name, Type.Name.ToString());
                    return(false);
                }

                for (var i = 0; i < PropScope.IdentifierList.Count; i++)
                {
                    var Ch = PropScope.IdentifierList[i];
                    if (Ch != null && Identifiers.IsLessAccessable(Id, Ch))
                    {
                        State.Messages.Add(MessageId.LessAccessable, Ch.Name, Id.Name.ToString());
                        return(false);
                    }
                }
            }
            else if (Id is Variable)
            {
                var Var  = Id as Variable;
                var Type = Id.Children[0];

                /*
                 *              if (Var is ConstVariable && Var.GlbInitVal == null)
                 *                      throw new ApplicationException("Constants must have a value");
                 */
                if (!IsLocal && Identifiers.IsLessAccessable(Type, Id))
                {
                    State.Messages.Add(MessageId.LessAccessable, Id.Name, Type.Name.ToString());
                    return(false);
                }
            }
            else if (Id is Function)
            {
                if (Id.Name.StartsWith("%Operator_"))
                {
                    if ((Id.Flags & IdentifierFlags.Static) == 0 || Id.Access != IdentifierAccess.Public)
                    {
                        State.Messages.Add(MessageId.OperatorModifiers, Id.Declaration);
                        return(false);
                    }

                    if (!(this is StructuredScope))
                    {
                        State.Messages.Add(MessageId.CannotDeclFunc, Id.Declaration);
                        return(false);
                    }
                }

                if ((Id is Constructor || Id is Destructor) && !(this is StructuredScope))
                {
                    State.Messages.Add(MessageId.CannotDeclFunc, Id.Declaration);
                    return(false);
                }

                var Func     = Id as Function;
                var FuncType = Func.TypeOfSelf.RealId as TypeOfFunction;
                var RetType  = FuncType.Children[0];

                if (Identifiers.IsLessAccessable(RetType, Id))
                {
                    State.Messages.Add(MessageId.LessAccessable, Id.Declaration, RetType.Name.ToString());
                    return(false);
                }

                var DefaultValues = false;
                var CantBeMore    = false;
                for (var i = 1; i < FuncType.Children.Length; i++)
                {
                    var e = FuncType.Children[i] as FunctionParameter;
                    if (Identifiers.IsLessAccessable(e.Children[0], Id))
                    {
                        State.Messages.Add(MessageId.LessAccessable, e.Declaration, e.TypeOfSelf.Name.ToString());
                        return(false);
                    }
                    else if (CantBeMore)
                    {
                        State.Messages.Add(MessageId.ParamArrayMustBeTheLast, e.Declaration);
                        return(false);
                    }
                    else if ((e.ParamFlags & ParameterFlags.ParamArray) != 0)
                    {
                        CantBeMore = true;
                    }
                    else if (e.ConstInitValue != null)
                    {
                        DefaultValues = true;
                    }
                    else if (DefaultValues)
                    {
                        State.Messages.Add(MessageId.ParamIsntOptional, e.Declaration);
                        return(false);
                    }
                }

                var Overload = Func.Overload;
                if (Overload.OverloadCount > 0)
                {
                    for (var i = 0; i < Overload.Functions.Count; i++)
                    {
                        var e = Overload.Functions[i];
                        if (e == Id)
                        {
                            continue;
                        }

                        if (Func is Constructor && e is Constructor)
                        {
                            if ((Func.Flags & IdentifierFlags.Static) != 0 && (e.Flags & IdentifierFlags.Static) != 0)
                            {
                                State.Messages.Add(MessageId.IdAlreadyDefined, Func.Declaration);
                                return(false);
                            }
                            else if ((Func.Flags & IdentifierFlags.Static) != 0 || (e.Flags & IdentifierFlags.Static) != 0)
                            {
                                continue;
                            }
                        }

                        if (Identifiers.AreParametersSame(Func, e))
                        {
                            State.Messages.Add(MessageId.SameOverloads, Id.Declaration);
                            return(false);
                        }
                    }
                }

                return(true);
            }
            else if (Id is IdentifierAlias)
            {
                if (Identifiers.IsLessAccessable(Id.RealId, Id))
                {
                    State.Messages.Add(MessageId.LessAccessable, Id.Declaration, Id.RealId.Name.ToString());
                    return(false);
                }
            }
            else if (!(Id is Type))
            {
                throw new NotImplementedException();
            }

            if (Id.Name.IsValid && (Id.Flags & IdentifierFlags.Override) == 0)
            {
                if (IsAlreadyDefined(Id.Name.ToString()))
                {
                    State.Messages.Add(MessageId.IdAlreadyDefined, Id.Declaration);
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        bool ProcessId(Identifier Id)
        {
            if ((Id.Flags & IdentifierFlags.Override) != 0)
            {
                var OverriddenId = (Identifier)null;
                if (Id is MemberFunction)
                {
                    var MemberFunc = Id as MemberFunction;
                    var List       = Identifiers.SearchBaseMember(null, StructuredType, Id.Name.ToString(),
                                                                  x =>
                    {
                        if (!(x is MemberFunction))
                        {
                            return(false);
                        }
                        if (x.Access != MemberFunc.Access)
                        {
                            return(false);
                        }
                        return(x.TypeOfSelf.IsEquivalent(Id.TypeOfSelf));
                    });

                    if (List.Count == 1)
                    {
                        OverriddenId            = List[0].Identifier;
                        MemberFunc.OverriddenId = OverriddenId;
                    }
                    else if (List.Count != 0)
                    {
                        throw new ApplicationException();
                    }
                }
                else if (Id is Property)
                {
                    var Property  = Id as Property;
                    var PropScope = Property.PropertyScope;

                    var List = Identifiers.SearchBaseMember(null, StructuredType, Id.Name.ToString(),
                                                            x =>
                    {
                        var xProperty = x as Property;
                        if (xProperty == null)
                        {
                            return(false);
                        }

                        var xScope  = xProperty.PropertyScope;
                        var xGetter = xScope.Getter;
                        var xSetter = xScope.Setter;

                        if ((x.Flags & IdentifierFlags.Static) != 0)
                        {
                            return(false);
                        }
                        if (x.Access != Property.Access)
                        {
                            return(false);
                        }
                        if ((PropScope.Getter == null) != (xGetter == null))
                        {
                            return(false);
                        }
                        if ((PropScope.Setter == null) != (xSetter == null))
                        {
                            return(false);
                        }

                        if (PropScope.Getter != null)
                        {
                            if (PropScope.Getter.Access != xGetter.Access)
                            {
                                return(false);
                            }
                            if (!PropScope.Getter.TypeOfSelf.IsEquivalent(xGetter.TypeOfSelf))
                            {
                                return(false);
                            }
                        }

                        if (PropScope.Setter != null)
                        {
                            if (PropScope.Setter.Access != xSetter.Access)
                            {
                                return(false);
                            }
                            if (!PropScope.Setter.TypeOfSelf.IsEquivalent(xSetter.TypeOfSelf))
                            {
                                return(false);
                            }
                        }

                        return(true);
                    });

                    if (List.Count == 1)
                    {
                        var OverriddenProp  = List[0].Identifier as Property;
                        var OverriddenScope = OverriddenProp.PropertyScope;
                        Property.OverriddenId = OverriddenProp;

                        if (PropScope.Getter != null)
                        {
                            var OverriddenFunc = OverriddenScope.Getter as MemberFunction;
                            var MemberGetter   = PropScope.Getter as MemberFunction;
                            MemberGetter.OverriddenId = OverriddenFunc;
                        }

                        if (PropScope.Setter != null)
                        {
                            var OverriddenFunc = OverriddenScope.Setter as MemberFunction;
                            var MemberSetter   = PropScope.Setter as MemberFunction;
                            MemberSetter.OverriddenId = OverriddenFunc;
                        }

                        OverriddenId = OverriddenProp;
                    }
                    else if (List.Count != 0)
                    {
                        throw new ApplicationException();
                    }
                }
                else
                {
                    throw new ApplicationException();
                }

                if (OverriddenId == null)
                {
                    State.Messages.Add(MessageId.NoOverridable, Id.Declaration);
                    return(false);
                }
                else if ((OverriddenId.Flags & IdentifierFlags.Virtual) == 0)
                {
                    State.Messages.Add(MessageId.OverrideNonvirtual, Id.Declaration);
                    return(false);
                }
                else if ((OverriddenId.Flags & IdentifierFlags.Sealed) != 0)
                {
                    State.Messages.Add(MessageId.OverrideSealed, Id.Declaration);
                    return(false);
                }
            }
            else if (!(Id is Constructor || Id is Destructor) && Id.Name.IsValid)
            {
                var List = new List <IdentifierFound>();
                for (var i = 0; i < StructuredType.BaseStructures.Length; i++)
                {
                    var Base = StructuredType.BaseStructures[i].Base;
                    List.AddRange(Identifiers.SearchMember(null, Base, Id.Name.ToString(),
                                                           x =>
                    {
                        if (x.DeclaredIdType == DeclaredIdType.Function &&
                            Id.DeclaredIdType == DeclaredIdType.Function)
                        {
                            if (!Identifiers.AreParametersSame(x, Id))
                            {
                                return(false);
                            }
                        }

                        return(x.Name.IsValid);
                    }
                                                           ));
                }

                if (List.Count > 0)
                {
                    if ((Id.Flags & IdentifierFlags.HideBaseId) == 0)
                    {
                        State.Messages.Add(MessageId.HidingRequired, Id.Name);
                    }
                }
                else
                {
                    if ((Id.Flags & IdentifierFlags.HideBaseId) != 0)
                    {
                        State.Messages.Add(MessageId.HidingUnnecessary, Id.Name);
                    }
                }
            }

            return(true);
        }