Example #1
0
        public ProtoCore.AST.AssociativeAST.ImportNode Import(string moduleName, string typeName, string alias, int line = -1, int col = -1)
        {
            curLine = line;
            curCol  = col;

            moduleName = moduleName.Replace("\"", String.Empty);
            ProtoCore.AST.AssociativeAST.ImportNode node = new ProtoCore.AST.AssociativeAST.ImportNode();
            node.ModuleName = moduleName;

            string modulePathFileName = FileUtils.GetDSFullPathName(moduleName, _coreObj.Options);

            // Tracking directory paths for all imported DS files during preload assembly stage so that they can be accessed by Graph compiler before execution - pratapa
            if (_coreObj.IsParsingPreloadedAssembly)
            {
                string dirName = Path.GetDirectoryName(modulePathFileName);
                if (!string.IsNullOrEmpty(dirName) && !_coreObj.Options.IncludeDirectories.Contains(dirName))
                {
                    _coreObj.Options.IncludeDirectories.Add(dirName);
                }
            }

            if (string.IsNullOrEmpty(typeName))
            {
                //Check if moduleName is a type name with namespace.
                Type type = Type.GetType(moduleName);
                if (type != null)
                {
                    typeName           = type.FullName;
                    modulePathFileName = type.Assembly.Location;
                }
            }

            if (modulePathFileName == null || !File.Exists(modulePathFileName))
            {
                System.Diagnostics.Debug.Write(@"Cannot import file: '" + modulePathFileName);
                _coreObj.LogWarning(ProtoCore.BuildData.WarningID.kFileNotFound, string.Format(Resources.kFileNotFound, modulePathFileName));
                return(null);
            }

            node.ModulePathFileName = modulePathFileName;
            node.CodeNode           = null;
            if (typeName.Length > 0)
            {
                node.Identifiers.Add(typeName);
            }

            ProtoCore.AST.AssociativeAST.ImportNode importedNode = null;
            if (TryGetImportNode(modulePathFileName, typeName, out importedNode))
            {
                node.HasBeenImported = true;
                return(node);
            }

            ProtoCore.AST.AssociativeAST.CodeBlockNode codeNode = null;
            if (importedNode == null)
            {
                codeNode = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            }
            else
            {
                codeNode = importedNode.CodeNode;
            }

            try
            {
                codeNode = ImportCodeBlock(modulePathFileName, typeName, alias, codeNode);
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    _coreObj.BuildStatus.LogSemanticError(ex.InnerException.Message);
                }
                _coreObj.BuildStatus.LogSemanticError(ex.Message);
            }

            //Cache the codeblock of root import node.
            CodeBlockNode rootImportCodeBlock = mRootImportNode.CodeNode;

            mRootImportNode.CodeNode = new CodeBlockNode(); //reset with the new one.

            //Remove all empty nodes and add to root import node.
            codeNode.Body.RemoveAll(AddToRootImportNodeIfEmpty);

            if (mRootImportNode.CodeNode.Body.Count == 0)       //empty
            {
                mRootImportNode.CodeNode = rootImportCodeBlock; //reset the old one.
            }
            if (importedNode != null)
            {
                //module has import node, but type is not imported yet.
                //MergeCodeBlockNode(ref importedNode, codeBlockNode);

                //update existing import node and return null.
                importedNode.CodeNode = codeNode;
                return(null);
            }

            node.CodeNode = codeNode;
            mModuleTable.Add(modulePathFileName, node);
            return(node);
        }