/* MakeModuleScopesVisitor - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
package alloy.semantic;
import java.util.HashMap;
import java.util.Iterator;

import alloy.ast.ASTDepthFirstVisitor;
import alloy.ast.Module;
import alloy.ast.Modules;
import alloy.ast.Paragraph;
import alloy.ast.Paragraphs;
import alloy.ast.QualifiedName;
import alloy.ast.Signature;
import alloy.util.Dbg;
import alloy.util.Msg;

public class MakeModuleScopesVisitor extends ASTDepthFirstVisitor
{
    private ModuleScope _curModuleScope;
    private Signature _curSig;
    private HashMap _moduleNameToModule = new HashMap();
    private ModuleScopeTable _moduleNameToModuleScope
	= ModuleScopeTable.getInstance();
    
    private void addUsedParagraphs(Paragraphs paragraphs) {
	Iterator iterator = paragraphs.getParagraphIter();
	while (iterator.hasNext()) {
	    Paragraph paragraph = (Paragraph) iterator.next();
	    QualifiedName qualifiedname = paragraph.getName();
	    try {
		_curModuleScope.addUsedParagraph(qualifiedname, paragraph);
	    } catch (AlreadyMappedException alreadymappedexception) {
		Paragraph paragraph_0_ = null;
		try {
		    paragraph_0_ = _curModuleScope.getParagraph(qualifiedname);
		} catch (ModuleScope.NoParagraphException noparagraphexception) {
		    Dbg.fatal("this can't happen");
		}
		Dbg.user(new Msg(("paragraph " + paragraph_0_.getName()
				  + " at " + paragraph_0_.getLocation()
				  + " conflicts with paragraph "
				  + paragraph.getName()),
				 paragraph));
	    }
	}
    }
    
    private void addOpenedParagraphs(Paragraphs paragraphs) {
	Iterator iterator = paragraphs.getParagraphIter();
	while (iterator.hasNext()) {
	    Paragraph paragraph = (Paragraph) iterator.next();
	    QualifiedName qualifiedname = paragraph.getName();
	    try {
		_curModuleScope.addOpenedParagraph(qualifiedname, paragraph);
	    } catch (AlreadyMappedException alreadymappedexception) {
		Paragraph paragraph_1_ = null;
		try {
		    paragraph_1_ = _curModuleScope.getParagraph(qualifiedname);
		} catch (ModuleScope.NoParagraphException noparagraphexception) {
		    Dbg.fatal("this can't happen");
		}
		Dbg.user(new Msg(("paragraph "
				  + paragraph_1_.getName().nodeString()
				  + " at " + paragraph_1_.getLocation()
				  + " conflicts with paragraph "
				  + paragraph.getName().nodeString()),
				 paragraph));
	    }
	}
    }
    
    public void visit(Modules modules) {
	Iterator iterator = modules.getModuleIter();
	while (iterator.hasNext()) {
	    Module module = (Module) iterator.next();
	    String string = module.getName().nodeString();
	    Dbg.chk(!_moduleNameToModule.containsKey(string),
		    "module appears twice in Modules");
	    _moduleNameToModule.put(string, module);
	}
	this.visit((alloy.ast.Node) modules);
    }
    
    public void visit(Module module) {
	_curModuleScope = new ModuleScope();
	addOpenedParagraphs(module.getSignatures());
	addOpenedParagraphs(module.getFacts());
	addOpenedParagraphs(module.getFunctions());
	addOpenedParagraphs(module.getAssertions());
	module.getSignatures().applyVisitor(this);
	Iterator iterator = module.getOpenedModules().getQualifiedNameIter();
	while (iterator.hasNext()) {
	    QualifiedName qualifiedname = (QualifiedName) iterator.next();
	    String string = qualifiedname.nodeString();
	    Module module_2_ = (Module) _moduleNameToModule.get(string);
	    if (module_2_ == null)
		Dbg.user(new Msg("opened non-existent module", qualifiedname));
	    else {
		addOpenedParagraphs(module_2_.getSignatures());
		addOpenedParagraphs(module_2_.getFacts());
		addOpenedParagraphs(module_2_.getFunctions());
		addOpenedParagraphs(module_2_.getAssertions());
	    }
	}
	Iterator iterator_3_ = module.getUsedModules().getQualifiedNameIter();
	while (iterator_3_.hasNext()) {
	    QualifiedName qualifiedname = (QualifiedName) iterator_3_.next();
	    String string = qualifiedname.nodeString();
	    Module module_4_ = (Module) _moduleNameToModule.get(string);
	    if (module_4_ == null)
		Dbg.user(new Msg("used non-existent module", qualifiedname));
	    else {
		addUsedParagraphs(module_4_.getSignatures());
		addUsedParagraphs(module_4_.getFacts());
		addUsedParagraphs(module_4_.getFunctions());
		addUsedParagraphs(module_4_.getAssertions());
	    }
	}
	_moduleNameToModuleScope.put(module.getName().nodeString(),
				     _curModuleScope);
    }
}
