Example #1
0
        protected virtual SyntaxNode VisitModuleSyntax(ModuleSyntax pNode)
        {
            List <MethodSyntax> methods = new List <MethodSyntax>(pNode.Methods.Count);

            foreach (var m in pNode.Methods)
            {
                methods.Add((MethodSyntax)Visit(m));
            }

            List <TypeDefinitionSyntax> definitions = new List <TypeDefinitionSyntax>(pNode.Structs.Count);

            foreach (var d in pNode.Structs)
            {
                definitions.Add((TypeDefinitionSyntax)Visit(d));
            }

            List <EnumSyntax> enums = new List <EnumSyntax>(pNode.Enums.Count);

            foreach (var e in pNode.Enums)
            {
                enums.Add((EnumSyntax)Visit(e));
            }

            List <DeclarationSyntax> fields = new List <DeclarationSyntax>(pNode.Fields.Count);

            foreach (var f in pNode.Fields)
            {
                fields.Add((DeclarationSyntax)Visit(f));
            }
            return(SyntaxFactory.Module(pNode.Imports, methods, definitions, enums, fields));
        }
        protected override void VisitModuleSyntax(ModuleSyntax pNode)
        {
            //Global scope
            _locals.AddScope();

            foreach (var f in pNode.Fields)
            {
                Visit(f);
            }

            //Infer methods
            foreach (var m in pNode.Methods)
            {
                Visit(m);
            }

            foreach (var s in pNode.Structs)
            {
                Visit(s.DeclaredType);
                Visit(s.AppliesTo);

                _unit.FromString(s.GetApplicableType(), out SmallType type);
                using (var st = Store.AddValue("__Struct", type))
                {
                    foreach (var m in s.Methods)
                    {
                        Visit(m);
                    }
                }
            }

            _locals.RemoveScope();
        }
Example #3
0
        protected override SyntaxNode VisitModuleSyntax(ModuleSyntax pNode)
        {
            _locals.AddScope();
            foreach (var f in pNode.Fields)
            {
                Visit(f);
            }

            //Find all methods we need to polymorph
            //A method needs to be polymorphed if any of it's parameters are traits
            foreach (var m in pNode.Methods)
            {
                foreach (var p in m.Parameters)
                {
                    if (p.Type.IsTrait)
                    {
                        if (!_methodsToPoly.ContainsKey(m.Name))
                        {
                            _methodsToPoly.Add(m.Name, new List <MethodSyntax>());
                        }
                        _methodsToPoly[m.Name].Add(m);
                        break;
                    }
                }
            }

            List <MethodSyntax> methods = new List <MethodSyntax>(pNode.Methods.Count);

            //Visit any non-poly nodes
            foreach (var m in pNode.Methods)
            {
                if (!_methodsToPoly.ContainsKey(m.Name))
                {
                    methods.Add((MethodSyntax)Visit(m));
                }
            }

            List <TypeDefinitionSyntax> types = new List <TypeDefinitionSyntax>(pNode.Structs.Count);

            foreach (var s in pNode.Structs)
            {
                types.Add((TypeDefinitionSyntax)Visit(s));
            }

            foreach (var v in _polydMethods.Values)
            {
                methods.AddRange(v);
            }

            _locals.RemoveScope();

            return(SyntaxFactory.Module(pNode.Imports, methods, types, pNode.Enums, pNode.Fields));
        }
        protected override void VisitModuleSyntax(ModuleSyntax pNode)
        {
            _locals = new ScopeCache <LocalReference>();
            _locals.AddScope();
            using (var v = Store.AddValue <string>("RunMethod", null))
            {
                base.VisitModuleSyntax(pNode);

                if (pNode == _mainModule && v.Value == null)
                {
                    CompilerErrors.NoRunMethod(pNode.Span);
                }
            }
            _locals.RemoveScope();
        }
        protected override SyntaxNode VisitModuleSyntax(ModuleSyntax pNode)
        {
            //Build our list for discovering types
            for (int i = 0; i < pNode.Structs.Count; i++)
            {
                var s = pNode.Structs[i];
                if (s.DefinitionType == DefinitionTypes.Implement)
                {
                    var applies = SyntaxHelper.GetFullTypeName(s.AppliesTo);
                    if (!_implements.ContainsKey(applies))
                    {
                        _implements.Add(applies, new List <TypeDefinitionSyntax>());
                    }
                    _implements[applies].Add(s);
                }
            }

            return(base.VisitModuleSyntax(pNode));
        }
Example #6
0
 protected virtual void VisitModuleSyntax(ModuleSyntax pNode)
 {
     foreach (var m in pNode.Methods)
     {
         Visit(m);
     }
     foreach (var d in pNode.Structs)
     {
         Visit(d);
     }
     foreach (var e in pNode.Enums)
     {
         Visit(e);
     }
     foreach (var f in pNode.Fields)
     {
         Visit(f);
     }
 }
Example #7
0
        protected override void VisitModuleSyntax(ModuleSyntax pNode)
        {
            //First we go through the current module to find any types that need to poly
            foreach (var s in pNode.Structs)
            {
                MaybeQueueStructToPoly(s);
            }

            //After we have found all of those, we go through the imported modules and find additional types
            //Since we could be using those types in our main module we need to poly them
            foreach (var r in _compilation.GetAllReferences())
            {
                foreach (var s in r.Module.Structs)
                {
                    MaybeQueueStructToPoly(s);
                }
            }

            base.VisitModuleSyntax(pNode);
        }
Example #8
0
        private static CompilationModule BuildInternal(ModuleSyntax pModule, string pNamespace)
        {
            CompilationModule main = new CompilationModule(pModule, pNamespace);

            Parallel.ForEach(pModule.Imports, (i) =>
            {
                var alias = i.Key;
                var node  = i.Value;
                var mod   = BuildInternal(node, alias);

                if (mod != null)
                {
                    main.Cache.AddReference(alias, mod);
                }
            });

            if (!main.Compile(main.Cache))
            {
                return(null);
            }

            return(main);
        }
 protected override void VisitWorkspaceSyntax(WorkspaceSyntax pNode)
 {
     _mainModule = pNode.Module;
     base.VisitWorkspaceSyntax(pNode);
 }
        protected override void VisitModuleSyntax(ModuleSyntax pNode)
        {
            //////
            ////// Discover enums
            ////// Add enums first since they can't reference anything, but things can reference enums
            //////
            foreach (var e in pNode.Enums)
            {
                _unit.AddType(e);
            }

            //Build our list for discovering types
            for (int i = 0; i < pNode.Structs.Count; i++)
            {
                var s = pNode.Structs[i];
                if (s.DefinitionType == DefinitionTypes.Implement)
                {
                    var applies = SyntaxHelper.GetFullTypeName(s.AppliesTo);
                    if (!_implements.ContainsKey(applies))
                    {
                        _implements.Add(applies, new List <TypeDefinitionSyntax>());
                    }
                    _implements[applies].Add(s);
                }
                else
                {
                    _discoveryGraph.AddNode(s);
                }
            }

            /////
            ///// Discover all other types
            /////
            var nodes = _discoveryGraph.GetNodes();

            for (int i = 0; i < nodes.Count; i++)
            {
                var t = SyntaxHelper.GetFullTypeName(nodes[i].Node.DeclaredType);
                if (!nodes[i].Permanent &&
                    !nodes[i].Temporary &&
                    DiscoverTypes(t, nodes[i].Node.Span))
                {
                    //If we discover a type go back to the beginning to see if any that were dependent
                    //on this can now be typed
                    i = -1;
                }
            }

            foreach (var i in _implements)
            {
                foreach (var s in i.Value)
                {
                    //Validate that the namespace and type exist
                    var applyName = SyntaxHelper.GetFullTypeName(s.AppliesTo);
                    var name      = SyntaxHelper.GetFullTypeName(s.DeclaredType);

                    //Mark any traits for types
                    if (ValidateType(s.AppliesTo.Namespace, applyName, s) &&
                        ValidateType(s.DeclaredType.Namespace, name, s))
                    {
                        var result = _unit.FromString(s.DeclaredType, out SmallType traitType);
                        System.Diagnostics.Debug.Assert(result == Compiler.FindResult.Found);

                        result = _unit.FromString(s.AppliesTo, out SmallType applyType);
                        System.Diagnostics.Debug.Assert(result == Compiler.FindResult.Found);

                        applyType.AddTrait(traitType);

                        TypeDefinitionSyntax trait;
                        if (s.DeclaredType.Namespace == null)
                        {
                            trait = _discoveryGraph.GetNode(name).Node;
                        }
                        else
                        {
                            //TODO clean this up
                            trait = _unit.GetReference(s.DeclaredType.Namespace.Value).Module.Structs.SingleOrDefault((pt) => pt.DeclaredType.Value == s.DeclaredType.Value);
                        }
                        ValidateImplementation(trait, s);
                    }
                }
            }

            //
            // Add all methods to the MethodCache
            //
            for (int j = 0; j < pNode.Methods.Count; j++)
            {
                AddMethodToCache(null, pNode.Methods[j], out MethodDefinition m);
            }

            //
            // Add struct methods to MethodCache
            //
            foreach (var s in pNode.Structs)
            {
                var result = _unit.FromString(s.GetApplicableType(), out SmallType type);

                //This can occur if we are specifying an undeclared type
                //or a type imported from another module
                // - The namespace might not be specified
                // - The type might not be exported
                switch (result)
                {
                case Compiler.FindResult.IncorrectScope:
                    CompilerErrors.TypeNotInScope(s.GetApplicableType().ToString(), s.Span);
                    break;

                case Compiler.FindResult.NotFound:
                    CompilerErrors.UndeclaredType(s.GetApplicableType().ToString(), s.Span);
                    break;
                }

                //Add each method to the cache and set type constructors if necessary
                for (int j = 0; j < s.Methods.Count; j++)
                {
                    if (AddMethodToCache(type, s.Methods[j], out MethodDefinition m) &&
                        s.Methods[j].Annotation.Value == KeyAnnotations.Constructor)
                    {
                        type.SetConstructor(m);
                    }
                }

                if (!type.HasDefinedConstructor())
                {
                    type.SetDefaultConstructor();
                }
            }
        }
Example #11
0
 public ModuleSyntax VisitModule(ModuleSyntax pNode)
 {
     return((ModuleSyntax)Visit(pNode));
 }
Example #12
0
 public CompilationModule(ModuleSyntax pModule, string pNamespace)
 {
     Cache  = new CompilationCache(pNamespace);
     Module = pModule;
 }