/* $Id: extern.c,v 1.1.1.1 2008/10/28 13:28:21 kiesling Exp $ */

/*
  This file is part of ctalk.
  Copyright © 2005-2008 Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This program 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 2 of the License, or
  (at your option) any later version.

  This program 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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation, 
  Inc., 51 Franklin St., Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <stdio.h>
#include <string.h>
#include "object.h"
#include "message.h"
#include "ctalk.h"
#include "lex.h"

extern HASHTAB declared_functions;     /* Declared in cvars.c.   */

int extern_declaration (MESSAGE_STACK messages, int keyword_ptr) {

  int lookahead;
  CVAR *c;
  CFUNC *cfn;

  if ((lookahead = scanforward (messages, keyword_ptr - 1, 
			   get_stack_top (messages), LABEL)) == ERROR)
    _error ("Parse error.\n");

  if (is_c_var_declaration_msg (messages, lookahead, FALSE)) {
    if ((c = parser_to_cvars ()) != NULL) {
      c -> attrs |= CVAR_ATTR_EXTERN;
      if ((c -> attrs & CVAR_ATTR_FN_DECL) ||
	  (c-> attrs & CVAR_ATTR_FN_PTR_DECL) ||
	  (c -> attrs & CVAR_ATTR_FN_PROTOTYPE)) {
	cfn = cvar_to_cfunc (c);
	_delete_cvar (c);
	unlink_global_cvar (c);
	_hash_put (declared_functions, (void *)cfn, cfn -> decl);
      } else {
	add_variable_from_cvar (c);
      }
    }
  }

  return SUCCESS;
}

