/*
 * moco, the Monty Compiler
 * Copyright (c) 2013-2014, Monty's Coconut, All rights reserved.
 *
 * This file is part of moco, the Monty Compiler.
 *
 * moco is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * moco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Linking this program and/or its accompanying libraries statically or
 * dynamically with other modules is making a combined work based on this
 * program. Thus, the terms and conditions of the GNU General Public License
 * cover the whole combination.
 *
 * As a special exception, the copyright holders of moco give
 * you permission to link this programm and/or its accompanying libraries
 * with independent modules to produce an executable, regardless of the
 * license terms of these independent modules, and to copy and distribute the
 * resulting executable under terms of your choice, provided that you also meet,
 * for each linked independent module, the terms and conditions of the
 * license of that module.
 *
 * An independent module is a module which is not
 * derived from or based on this program and/or its accompanying libraries.
 * If you modify this library, you may extend this exception to your version of
 * the program or library, but you are not obliged to do so. If you do not wish
 * to do so, delete this exception statement from your version.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library.
 */
package de.uni.bremen.monty.moco.ast;

import java.util.BitSet;

/** Baseclass for every node in the AST. */
public abstract class BasicASTNode implements ASTNode {

	public static final int NUMBER_OF_VISITORS = 6;

	/** Sourcecode position of this node. */
	private final Position position;

	/** Parent node. */
	private ASTNode parentNode;

	/** Associated scope. */
	private Scope scope;

	private BitSet visitedFlags = new BitSet(NUMBER_OF_VISITORS);

	private boolean isNativeNode = false;

	/** Constructor.
	 *
	 * @param position
	 *            Position of this node */
	public BasicASTNode(Position position) {
		this.position = position;
	}

	/** {@inheritDoc} */
	@Override
	public String toString() {
		return getClass().getSimpleName();
	}

	/** Get parent node.
	 *
	 * @return the parent node */
	@Override
	public ASTNode getParentNode() {
		return parentNode;
	}

	/** Get parent node.
	 *
	 * @return the parent node */
	public ASTNode getParentNodeByType(Class type) {
		ASTNode n = getParentNode();
		while ((!type.isInstance(n)) && (n != null)) {
			n = n.getParentNode();
		}
		return n;
	}

	/** Set parent node.
	 *
	 * @param parentNode
	 *            the parent node */
	@Override
	public void setParentNode(ASTNode parentNode) {
		this.parentNode = parentNode;
	}

	/** Get the sourcecode position.
	 *
	 * @return the position */
	@Override
	public Position getPosition() {
		return position;
	}

	/** Set the associated scope.
	 *
	 * @param scope
	 *            the associated scope */
	@Override
	public void setScope(Scope scope) {

		this.scope = scope;
	}

	/** Get the accociated scope.
	 *
	 * @return the scope */
	@Override
	public Scope getScope() {
		return scope;
	}

	@Override
	public BitSet getVisitedFlags() {
		return visitedFlags;
	}

	public boolean isNative() {
		return isNativeNode;
	}

	public void setNative(boolean isNative) {
		isNativeNode = isNative;
	}
}
