Ejemplo n.º 1
0
        public override AstNode Visit(FieldDefinition node)
        {
            // Add the parent static flag.
            if (currentContainer.IsStatic())
            {
                MemberFlags oldInstance = node.GetFlags() & MemberFlags.InstanceMask;
                if (oldInstance == MemberFlags.Static || oldInstance == MemberFlags.Instanced)
                {
                    MemberFlags flags = node.GetFlags() & ~MemberFlags.InstanceMask;
                    node.SetFlags(flags | MemberFlags.Static);
                }
                else
                {
                    Error(node, "static class member cannot {0}.", oldInstance.ToString().ToLower());
                }
            }

            // Add the parent unsafe flag.
            if (IsUnsafe)
            {
                MemberFlags flags = node.GetFlags() & ~MemberFlags.SecurityMask;
                node.SetFlags(flags | MemberFlags.Unsafe);
            }

            return(node);
        }
Ejemplo n.º 2
0
        public override AstNode Visit(PropertyDefinition node)
        {
            // Add the parent static flag.
            if (currentContainer.IsStatic())
            {
                MemberFlags oldInstance = node.GetFlags() & MemberFlags.InstanceMask;
                if (oldInstance == MemberFlags.Static || oldInstance == MemberFlags.Instanced)
                {
                    MemberFlags flags = node.GetFlags() & ~MemberFlags.InstanceMask;
                    node.SetFlags(flags | MemberFlags.Static);
                }
                else
                {
                    Error(node, "static class member cannot {0}.", oldInstance.ToString().ToLower());
                }
            }

            // Add the parent unsafe flag.
            if (IsUnsafe)
            {
                MemberFlags flags = node.GetFlags() & ~MemberFlags.SecurityMask;
                node.SetFlags(flags | MemberFlags.Unsafe);
            }

            // Link the accessors.
            AstNode acc = node.GetAccessors();

            while (acc != null)
            {
                PropertyAccessor pacc = (PropertyAccessor)acc;
                if (pacc.IsGetAccessor())
                {
                    if (node.GetAccessor != null)
                    {
                        Error(node, "a property only can have one get accessor.");
                    }
                    node.GetAccessor = (GetAccessorDefinition)pacc;
                }
                else
                {
                    if (node.SetAccessor != null)
                    {
                        Error(node, "a property only can have one set accessor.");
                    }
                    node.SetAccessor = (SetAccessorDefinition)pacc;
                }

                acc = acc.GetNext();
            }

            return(node);
        }
Ejemplo n.º 3
0
        public override AstNode Visit(EventDefinition node)
        {
            // Add the parent static flag.
            if (currentContainer.IsStatic())
            {
                MemberFlags oldInstance = node.GetFlags() & MemberFlags.InstanceMask;
                if (oldInstance == MemberFlags.Static || oldInstance == MemberFlags.Instanced)
                {
                    MemberFlags flags = node.GetFlags() & ~MemberFlags.InstanceMask;
                    node.SetFlags(flags | MemberFlags.Static);
                }
                else
                {
                    Error(node, "static class member cannot {0}.", oldInstance.ToString().ToLower());
                }
            }

            // Link the accessors.
            AstNode acc = node.GetAccessors();

            while (acc != null)
            {
                EventAccessorDefinition eacc = (EventAccessorDefinition)acc;
                string accName = eacc.GetName();
                if (accName == "add")
                {
                    if (node.AddAccessor != null)
                    {
                        Error(node, "an event only can have one add accessor.");
                    }
                    node.AddAccessor = eacc;
                }
                else if (accName == "remove")
                {
                    if (node.RemoveAccessor != null)
                    {
                        Error(node, "an event only can have one remove accessor.");
                    }
                    node.RemoveAccessor = eacc;
                }
                else
                {
                    Error(acc, "invalid event accessor name.");
                }

                acc = acc.GetNext();
            }

            return(node);
        }
Ejemplo n.º 4
0
        public override AstNode Visit(ClassDefinition node)
        {
            // Check the partial flag.
            bool isPartial = (node.GetFlags() & MemberFlags.ImplFlagMask) == MemberFlags.Partial;

            // Add the parent static flag.
            if (currentContainer.IsStatic())
            {
                MemberFlags oldInstance = node.GetFlags() & MemberFlags.InstanceMask;
                if (oldInstance == MemberFlags.Static || oldInstance == MemberFlags.Instanced)
                {
                    MemberFlags flags = node.GetFlags() & ~MemberFlags.InstanceMask;
                    node.SetFlags(flags | MemberFlags.Static);
                }
                else
                {
                    Error(node, "static class member cannot be {0}.", oldInstance.ToString().ToLower());
                }
            }

            // Static classes are automatically sealed.
            if ((node.GetFlags() & MemberFlags.InstanceMask) == MemberFlags.Static)
            {
                MemberFlags flags = node.GetFlags() & ~MemberFlags.InheritanceMask;
                node.SetFlags(flags | MemberFlags.Sealed);
            }

            // Parse the generic signature.
            GenericSignature genericSign = node.GetGenericSignature();
            GenericPrototype genProto    = GenericPrototype.Empty;
            PseudoScope      protoScope  = null;

            if (genericSign != null)
            {
                // Visit the generic signature.
                genericSign.Accept(this);

                // Connect the generic prototype.
                genProto = genericSign.GetPrototype();

                // Create the placeholders scope.
                protoScope = new PseudoScope(currentScope, genProto);
                node.SetGenericScope(protoScope);
            }

            // Prevent redefinition.
            ScopeMember old = currentContainer.FindType(node.GetName(), genProto);

            if (old != null && (!isPartial || !old.IsClass()))
            {
                Error(node, "trying to redefine a class.");
            }

            // Create the structure
            Class clazz = (Class)old;

            if (clazz != null)
            {
                if (!clazz.IsPartial())
                {
                    Error(node, "incompatible partial class definitions.");
                }
            }
            else
            {
                clazz          = new Class(node.GetName(), node.GetFlags(), currentContainer);
                clazz.Position = node.GetPosition();
                clazz.SetGenericPrototype(genProto);
            }
            node.SetStructure(clazz);

            // Use the prototype scope.
            if (protoScope != null)
            {
                PushScope(protoScope);
            }

            // Register type maps.
            string     fullName = clazz.GetFullName();
            IChelaType alias;

            if (typeMaps.TryGetValue(fullName, out alias))
            {
                currentModule.RegisterTypeMap(alias, clazz);
            }

            // Register type association.
            IChelaType assoc;

            if (assocClasses.TryGetValue(fullName, out assoc))
            {
                currentModule.RegisterAssociatedClass(assoc, clazz);
            }

            // Array base class is special.
            if (fullName == "Chela.Lang.Array")
            {
                currentModule.RegisterArrayClass(clazz);
            }
            else if (fullName == "Chela.Lang.Attribute")
            {
                currentModule.RegisterAttributeClass(clazz);
            }
            else if (fullName == "Chela.Lang.ValueType")
            {
                currentModule.RegisterValueTypeClass(clazz);
            }
            else if (fullName == "Chela.Lang.Enum")
            {
                currentModule.RegisterEnumClass(clazz);
            }
            else if (fullName == "Chela.Lang.Type")
            {
                currentModule.RegisterTypeClass(clazz);
            }
            else if (fullName == "Chela.Lang.Delegate")
            {
                currentModule.RegisterDelegateClass(clazz);
            }
            else if (fullName == "Chela.Compute.StreamHolder")
            {
                currentModule.RegisterStreamHolderClass(clazz);
            }
            else if (fullName == "Chela.Compute.StreamHolder1D")
            {
                currentModule.RegisterStreamHolder1DClass(clazz);
            }
            else if (fullName == "Chela.Compute.StreamHolder2D")
            {
                currentModule.RegisterStreamHolder2DClass(clazz);
            }
            else if (fullName == "Chela.Compute.StreamHolder3D")
            {
                currentModule.RegisterStreamHolder3DClass(clazz);
            }
            else if (fullName == "Chela.Compute.UniformHolder")
            {
                currentModule.RegisterUniformHolderClass(clazz);
            }

            // Add into the current scope.
            if (currentContainer.IsNamespace())
            {
                Namespace space = (Namespace)currentContainer;
                if (old == null)
                {
                    space.AddType(clazz);
                }
            }
            else if (currentContainer.IsStructure() || currentContainer.IsInterface() || currentContainer.IsClass())
            {
                Structure parent = (Structure)currentContainer;
                if (old == null)
                {
                    parent.AddType(clazz);
                }
            }
            else
            {
                Error(node, "unexpected place for a class.");
            }

            // Push the class unsafe.
            if (clazz.IsUnsafe())
            {
                PushUnsafe();
            }

            // Update the scope.
            PushScope(clazz);

            // Visit the building children.
            VisitList(node.GetChildren());

            // Restore the scope.
            PopScope();

            // Restore the scope.
            if (protoScope != null)
            {
                PopScope();
            }

            // Pop the class unsafe.
            if (clazz.IsUnsafe())
            {
                PopUnsafe();
            }

            return(node);
        }