implement typedef

This commit is contained in:
Bill Currie 2002-01-23 20:50:25 +00:00
parent b7eded6854
commit 6a393d175e
4 changed files with 61 additions and 9 deletions

View file

@ -32,6 +32,8 @@
#ifndef __type_h
#define __type_h
void new_typedef (const char *name, struct type_s *type);
struct type_s *get_typedef (const char *name);
struct type_s *pointer_type (struct type_s *aux);
void print_type (struct type_s *type);

View file

@ -36,6 +36,7 @@ static const char rcsid[] =
#include "qfcc.h"
#include "scope.h"
#include "struct.h"
#include "type.h"
#include "qc-parse.h"
#define YY_NO_UNPUT
@ -242,6 +243,7 @@ static keyword_t keywords[] = {
{"NIL", NIL, 0, PROG_ID_VERSION},
{"struct", STRUCT, 0, PROG_VERSION},
{"enum", ENUM, 0, PROG_ID_VERSION},
{"typedef", TYPEDEF, 0, PROG_ID_VERSION},
};
static const char *
@ -269,8 +271,7 @@ type_or_name (char *token)
yylval.type = keyword->type;
return keyword->value;
}
type = find_struct (token);
if (type) {
if ((type = find_struct (token)) || (type = get_typedef (token))) {
yylval.type = type;
return TYPE;
}

View file

@ -110,7 +110,7 @@ typedef struct {
%token LOCAL RETURN WHILE DO IF ELSE FOR BREAK CONTINUE ELIPSIS NIL
%token IFBE IFB IFAE IFA
%token SWITCH CASE DEFAULT STRUCT ENUM
%token SWITCH CASE DEFAULT STRUCT ENUM TYPEDEF
%token <type> TYPE
%type <type> type opt_func func_parms array_decl
@ -160,13 +160,15 @@ def
{ current_type = build_type ($1, $4); } func_def_list
| STRUCT NAME
{ struct_type = new_struct ($2); } '=' '{' struct_defs '}'
| ENUM '{' enum_list opt_comma '}' opt_name
| ENUM '{' enum_list opt_comma '}'
{ process_enum ($3); }
;
opt_name
: /* empty */
| NAME {}
| TYPEDEF type NAME
{ new_typedef ($3, $2); }
| TYPEDEF ENUM '{' enum_list opt_comma '}' NAME
{
process_enum ($4);
new_typedef ($7, &type_integer);
}
;
struct_defs
@ -342,6 +344,8 @@ param
type
: opt_field TYPE { current_type = $2; } opt_func
{ $$ = build_type ($1, $4 ? $4 : $2); }
| opt_field TYPE { current_type = $2; } array_decl
{ $$ = build_type ($1, $4); }
;
opt_var_initializer

View file

@ -32,9 +32,54 @@ static const char rcsid[] =
#include <stdlib.h>
#include "QF/hash.h"
#include "qfcc.h"
typedef struct {
const char *name;
type_t *type;
} typedef_t;
static hashtab_t *typedef_hash;
static const char *
typedef_get_key (void *t, void *unused)
{
return ((typedef_t *)t)->name;
}
void
new_typedef (const char *name, type_t *type)
{
typedef_t *td;
if (!typedef_hash)
typedef_hash = Hash_NewTable (1023, typedef_get_key, 0, 0);
td = Hash_Find (typedef_hash, name);
if (td) {
error (0, "%s redefined", name);
return;
}
td = malloc (sizeof (typedef_t));
td->name = name;
td->type = type;
Hash_Add (typedef_hash, td);
}
type_t *
get_typedef (const char *name)
{
typedef_t *td;
if (!typedef_hash)
return 0;
td = Hash_Find (typedef_hash, name);
if (!td)
return 0;
return td->type;
}
type_t *
pointer_type (type_t *aux)
{