/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *               Stefan Tiran (AIT)
  *
  */


package org.momut.ooas.ast.identifiers;

import java.util.ArrayList;
import java.util.List;

import org.antlr.runtime.Token;
import org.momut.ooas.ast.IScope;
import org.momut.ooas.ast.statements.Statement;
import org.momut.ooas.ast.types.Type;
import org.momut.ooas.utils.exceptions.InternalCompilerException;

public abstract class FunctionIdentifier extends Scope
{
	private Statement m_body;
	private ArrayList<ParameterIdentifier> m_parameter = new ArrayList<ParameterIdentifier>();

	public Statement body() { return m_body; }
	public List<ParameterIdentifier> parameter() { return m_parameter; }


	public FunctionIdentifier(Token aToken,
			Type aType,
			IdentifierKind aKind,
			IScope aDefiningScope)
	{
		super (aToken, aType, aKind, aDefiningScope);
	}

	public FunctionIdentifier(FunctionIdentifier toCopy)
	{
		super (toCopy);
		m_body = toCopy.m_body;
		m_parameter = new ArrayList<ParameterIdentifier>(toCopy.m_parameter);
	}

	public FunctionIdentifier(
			String name,
			Type aType,
			IdentifierKind aKind,
			IScope aDefiningScope)
	{
		super(name, aType, aKind, aDefiningScope);
	}

	public void SetBody(Statement body)
	{
		m_body = body;
		if (m_body instanceof IScope) {
			final IScope s = (IScope) m_body;
			if (s.GetParentScope() == null)
				s.SetParentScope(this);
			else if (s.GetParentScope() != this)
				throw new InternalCompilerException("Internal Error: Methodbody has parent set to different method than it was added to.");
		}
	}

	public void AddParameter(ParameterIdentifier aParameter)
	{
		m_parameter.add(aParameter);
		AddIdentifier(aParameter, null);
	}
}
