package carray.ast;

import java.util.LinkedList;
import java.util.List;

import polyglot.ast.Import;
import polyglot.ast.Node;
import polyglot.ast.NodeFactory;
import polyglot.ast.SourceFile;
import polyglot.translate.ExtensionRewriter;
import polyglot.types.SemanticException;
import polyglot.util.Position;
import polyglot.util.SerialVersionUID;
import carray.translate.CArrayRewriter;

public class CArraySourceFileExt extends CArrayExt {
    private static final long serialVersionUID = SerialVersionUID.generate();

    @Override
    public Node extRewrite(ExtensionRewriter rw) throws SemanticException {
        CArrayRewriter crw = (CArrayRewriter) rw;
        if (crw.translcateCArray()) {
            SourceFile n = (SourceFile) super.extRewrite(rw);
            List<Import> imports = new LinkedList<>(n.imports());
            NodeFactory nf = rw.nodeFactory();
            // Add "import carray.CArray.*;".
            imports.add(nf.Import(Position.COMPILER_GENERATED,
                                  Import.TYPE_IMPORT_ON_DEMAND,
                                  "carray.CArray"));
            return n.imports(imports);
        }
        return super.extRewrite(rw);
    }
}
