/* $Id: cvar.h,v 1.1.1.1 2008/10/28 13:28:20 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.
*/

#ifndef _OBJECT_H
#include "object.h"
#endif

#ifndef _PVAL_H
#include "pval.h"
#endif

#ifndef _CVAR_H
#define _CVAR_H

/*
 *  C99 translation limit.
 */
#define MAX_DECLARATORS 12

#define IS_SIGN_LABEL(s) (!strcmp (s, "signed") || \
                          !strcmp (s, "unsigned"))
#define IS_TYPE_LENGTH(s) (!strcmp (s, "long") || \
                           !strcmp (s, "short"))
#define IS_SCALAR_TYPE(s) (!strcmp (s, "char") || \
                           !strcmp (s, "double") || \
                           !strcmp (s, "float") || \
                           !strcmp (s, "int") || \
                           !strcmp (s, "_Bool") || \
                           !strcmp (s, "_Complex") || \
                           !strcmp (s, "_Imaginary"))
#define IS_AGGREGATE_TYPE(s) (!strcmp (s, "enum") || \
                              !strcmp (s, "struct") || \
                              !strcmp (s, "union"))
typedef struct _cvar CVAR;

typedef enum {
  auto_sc_decl,
  const_sc_decl,
  extern_sc_decl,
  inline_sc_decl,
  register_sc_decl,
  static_sc_decl,
  volatile_sc_decl,
  typedef_sc_decl         /* Include typedef here even though it's not
			     a storage class. */
} STORAGE_CLASS;
  

#define CVAR_ATTR_STRUCT             (1 << 0)
#define CVAR_ATTR_STRUCT_INCOMPLETE  (1 << 1)
#define CVAR_ATTR_STRUCT_DECL        (1 << 2)
#define CVAR_ATTR_STRUCT_MBR         (1 << 3)
#define CVAR_ATTR_STRUCT_PTR         (1 << 4)
#define CVAR_ATTR_FN_DECL            (1 << 5)
#define CVAR_ATTR_FN_PTR_DECL        (1 << 6)
#define CVAR_ATTR_FN_PARAM           (1 << 7)
#define CVAR_ATTR_FN_PROTOTYPE       (1 << 8)
#define CVAR_ATTR_TYPEDEF            (1 << 9)
#define CVAR_ATTR_EXTERN             (1 << 10)
#define CVAR_ATTR_TYPEDEF_INCOMPLETE (1 << 11)
#define CVAR_ATTR_ENUM               (1 << 12)

#define CVAR_TYPE_CHAR               (1 << 1)
#define CVAR_TYPE_CONST              (1 << 2)
#define CVAR_TYPE_DOUBLE             (1 << 3)
#define CVAR_TYPE_ENUM               (1 << 4)
#define CVAR_TYPE_EXTERN             (1 << 5)
#define CVAR_TYPE_FLOAT              (1 << 6)
#define CVAR_TYPE_INLINE             (1 << 7)
#define CVAR_TYPE_INT                (1 << 8)
#define CVAR_TYPE_LONG               (1 << 9)
#define CVAR_TYPE_REGISTER           (1 << 10)
#define CVAR_TYPE_SHORT              (1 << 11)
#define CVAR_TYPE_SIGNED             (1 << 12)
#define CVAR_TYPE_STATIC             (1 << 13)
#define CVAR_TYPE_STRUCT             (1 << 14)
#define CVAR_TYPE_UNION              (1 << 15)
#define CVAR_TYPE_UNSIGNED           (1 << 16)
#define CVAR_TYPE_VOID               (1 << 17)
#define CVAR_TYPE_VOLATILE           (1 << 18)
#define CVAR_TYPE_BOOL               (1 << 19)
#define CVAR_TYPE_COMPLEX            (1 << 20)
#define CVAR_TYPE_IMAGINARY          (1 << 21)
#define CVAR_TYPE_LONGLONG           (1 << 22)
#ifdef __GNUC__
#define CVAR_TYPE_GNU_COMPLEX        (1 << 23)
#define CVAR_TYPE_GNU_INLINE         (1 << 24)
#endif

struct _cvar {
  char sig[4];
  char decl[MAXLABEL];
  char type[MAXLABEL];
  char qualifier[MAXLABEL];
  char qualifier2[MAXLABEL];
  char qualifier3[MAXLABEL];
  char qualifier4[MAXLABEL];
  char storage_class[MAXLABEL];
  char name[MAXLABEL];
  int n_derefs;
  int attrs, 
    type_attrs;
  Boolean is_unsigned;
  int scope;
  VAL val;                /* Used at run time to evaluate expressions
			     and for enum values.                     */
  struct _cvar *members;
  struct _cvar *params;
  struct _cvar *next;
  struct _cvar *prev;
};

typedef struct _cfunc {
  char decl[MAXLABEL];
  char return_type[MAXLABEL];
  char qualifier_type[MAXLABEL];
  char qualifier2_type[MAXLABEL];
  char storage_class[MAXLABEL];
  int is_unsigned;
  int return_derefs;
  int is_ptr_decl;
  int is_prototype_decl;
  int return_type_attrs;
  CVAR *params;
  struct _cfunc *next;
  struct _cfunc *prev;
} CFUNC;

typedef struct _varname {
  char name[MAXLABEL];
  Boolean ptr, 
    ptrptr;
  int n_derefs;
} VARNAME;

typedef struct _fn_declarator {
  char name[MAXLABEL];
  int attr;
} FN_DECLARATOR;

typedef CVAR** CVARREF_T;
#define CVARREF(__o) (&(__o))

#endif /* _CVAR_H */
