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

import alloy.ast.ASTDepthFirstVisitor;
import alloy.ast.Assertion;
import alloy.ast.Decl;
import alloy.ast.Fact;
import alloy.ast.FormulaPara;
import alloy.ast.Function;
import alloy.ast.Id;
import alloy.ast.Ids;
import alloy.ast.Module;
import alloy.ast.Signature;
import alloy.ast.Variable;
import alloy.util.Dbg;
import alloy.util.Msg;

public class NoParagraphNameShadowingVisitor extends ASTDepthFirstVisitor
{
    private ModuleScopeTable _moduleScopeTable
	= ModuleScopeTable.getInstance();
    private ModuleScope _curModuleScope;
    
    public void visit(Module module) {
	_curModuleScope = _moduleScopeTable.get(module.getName().nodeString());
	module.getSignatures().applyVisitor(this);
	module.getFunctions().applyVisitor(this);
	module.getFacts().applyVisitor(this);
	module.getAssertions().applyVisitor(this);
    }
    
    public void visit(Signature signature) {
	signature.getTypeParams().applyVisitor(this);
	signature.getDecls().applyVisitor(this);
	Fact fact = signature.getFact();
	if (!fact.isEmpty())
	    fact.applyVisitor(this);
    }
    
    public void visit(FormulaPara formulapara) {
	formulapara.getTypeParams().applyVisitor(this);
	formulapara.getFormula().applyVisitor(this);
    }
    
    public void visit(Fact fact) {
	visit((FormulaPara) fact);
    }
    
    public void visit(Assertion assertion) {
	visit((FormulaPara) assertion);
    }
    
    public void visit(Function function) {
	visit((FormulaPara) function);
	function.getArgList().getDecls().applyVisitor(this);
    }
    
    public void visit(Decl decl) {
	decl.getVariables().applyVisitor(this);
    }
    
    public void visit(Ids ids) {
	Iterator iterator = ids.getIdIter();
	while (iterator.hasNext())
	    noShadow((Id) iterator.next());
    }
    
    public void visit(Variable variable) {
	noShadow(variable.getId());
    }
    
    private void noShadow(Id id) {
	try {
	    _curModuleScope.getParagraph(id.nodeString());
	    Dbg.user(new Msg("shadowing of paragraph name", id));
	} catch (ModuleScope.MultipleMappingsException multiplemappingsexception) {
	    Dbg.user(new Msg("shadowing of paragraph name", id));
	    Dbg.user(new Msg(multiplemappingsexception.mappingsString(), id));
	} catch (ModuleScope.NoParagraphException noparagraphexception) {
	    /* empty */
	}
    }
}
