コード例 #1
0
ファイル: BindNamespaces.cs プロジェクト: codehaus/boo
        override public void Run()
        {
            NameResolutionService.Reset();

            foreach (Boo.Lang.Compiler.Ast.Module module in CompileUnit.Modules)
            {
                foreach (Import import in module.Imports)
                {
                    IEntity entity = NameResolutionService.ResolveQualifiedName(import.Namespace);
                    if (null == entity)
                    {
                        Errors.Add(CompilerErrorFactory.InvalidNamespace(import));
                        entity = TypeSystemServices.ErrorEntity;
                    }
                    else
                    {
                        if (!IsValidNamespace(entity))
                        {
                            Errors.Add(CompilerErrorFactory.NotANamespace(import, entity.FullName));
                            entity = TypeSystemServices.ErrorEntity;
                        }
                        else
                        {
                            if (null != import.AssemblyReference)
                            {
                                NamespaceEntity nsInfo = entity as NamespaceEntity;
                                if (null != nsInfo)
                                {
                                    entity = new AssemblyQualifiedNamespaceEntity(GetBoundAssembly(import.AssemblyReference), nsInfo);
                                }
                            }
                            if (null != import.Alias)
                            {
                                entity = new AliasedNamespace(import.Alias.Name, entity);
                                import.Alias.Entity = entity;
                            }
                        }
                    }

                    _context.TraceInfo("{1}: import reference '{0}' bound to {2}.", import, import.LexicalInfo, entity.Name);
                    import.Entity = entity;
                }
            }
        }
コード例 #2
0
        public override void OnImport(Boo.Lang.Compiler.Ast.Import import)
        {
            INamespace oldns  = NameResolutionService.CurrentNamespace;
            IEntity    entity = null;

            try
            {
                NameResolutionService.EnterNamespace(NameResolutionService.CurrentNamespace.ParentNamespace);
                entity = NameResolutionService.ResolveQualifiedName(import.Namespace);
            }
            finally
            {
                NameResolutionService.EnterNamespace(oldns);
            }

            if (null == entity)
            {
                entity = NameResolutionService.ResolveQualifiedName(import.Namespace);
            }

            //if 'import X', try 'import X from X'
            //comment out next if block if this is not wanted
            if (null == entity && null == import.AssemblyReference)
            {
                if (TryAutoAddAssemblyReference(import))
                {
                    entity = NameResolutionService.ResolveQualifiedName(import.Namespace);
                }
            }

            if (null == entity)
            {
                Errors.Add(CompilerErrorFactory.InvalidNamespace(import));
                entity = TypeSystemServices.ErrorEntity;
            }
            else
            {
                if (!IsValidNamespace(entity))
                {
                    Errors.Add(CompilerErrorFactory.NotANamespace(import, entity.FullName));
                    entity = TypeSystemServices.ErrorEntity;
                }
                else
                {
                    string name = entity.FullName;
                    if (null != import.AssemblyReference)
                    {
                        NamespaceEntity nsInfo = entity as NamespaceEntity;
                        if (null != nsInfo)
                        {
                            entity = new AssemblyQualifiedNamespaceEntity(GetBoundAssembly(import.AssemblyReference), nsInfo);
                        }
                    }

                    if (null != import.Alias)
                    {
                        entity = new AliasedNamespace(import.Alias.Name, entity);
                        import.Alias.Entity = entity;
                        name = entity.Name;                         //use alias name instead of namespace name
                    }

                    //only add unique namespaces
                    Import cachedImport = nameSpaces[name] as Import;
                    if (cachedImport == null)
                    {
                        nameSpaces[name] = import;
                    }
                    else
                    {
                        //ignore for partial classes in separate files
                        if (cachedImport.LexicalInfo.FileName == import.LexicalInfo.FileName)
                        {
                            Warnings.Add(CompilerWarningFactory.DuplicateNamespace(
                                             import, import.Namespace));
                        }
                        RemoveCurrentNode();
                        return;
                    }
                }
            }

            _context.TraceInfo("{1}: import reference '{0}' bound to {2}.", import, import.LexicalInfo, entity.FullName);
            import.Entity = entity;
        }