more work on parser
authorMatthias Braun <matze@braunis.de>
Mon, 18 Jun 2007 13:18:47 +0000 (13:18 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 18 Jun 2007 13:18:47 +0000 (13:18 +0000)
[r18330]

15 files changed:
Makefile
adt/hashset.c
adt/pset.c [deleted file]
ast.c
ast.h
ast_t.h
lexer.c
parser.c
parser.h
symbol.h
symbol_table.c
token.c
tokens.inc
type.c
type_hash.c

index ea97557..ab48de4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,19 +2,18 @@ GOAL = cparser
 
 #FIRM_CFLAGS = `pkg-config --cflags libfirm`
 #FIRM_LIBS = `pkg-config --libs libfirm`
-FIRM_CFLAGS = -I$(HOME)/projects/firm/libfirm/include -I$(HOME)/projects/firm/libcore
-FIRM_LIBS = -L$(HOME)/projects/firm/build/i686-pc-linux-gnu/debug -lfirm -llpp -lcore -lm
+FIRM_CFLAGS =
+FIRM_LIBS = -L. -lfirm
 
-CFLAGS += -Wall -W -Wextra -Werror -O0 -g3 -std=c99
+CFLAGS += -Wall -W -Wextra -Werror -O0 -g3 -std=c99 -pedantic
 CFLAGS += -DHAVE_CONFIG_H
 CFLAGS += -I .
 CFLAGS += $(FIRM_CFLAGS)
 
-LFLAGS = $(FIRM_LIBS) -llpp -ldl --export-dynamic -g3
+LFLAGS = -g3 $(FIRM_LIBS)
 
 SOURCES := \
        adt/hashset.c \
-       adt/pset.c \
        adt/strset.c \
        adt/xmalloc.c \
        ast.c \
index c2ac584..108a3bc 100644 (file)
@@ -593,4 +593,6 @@ void hashset_remove_iterator(HashSet *this, const HashSetIterator *iter)
        this->consider_shrink = 1;
 }
 
+#else
+__attribute__((unused)) static int dummy;
 #endif
diff --git a/adt/pset.c b/adt/pset.c
deleted file mode 100644 (file)
index 96fb187..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/* collides with libfirm */
-#if 0
-
-#include <config.h>
-
-#include "pset.h"
-
-/** probing method: quadratic probing */
-#define DO_REHASH
-#define HashSet                    pset_t
-#define HashSetIterator            pset_iterator_t
-#define ValueType                  void*
-#define NullValue                  NULL
-#define DeletedValue               ((void*)-1)
-#define KeysEqual(this,key1,key2)  1
-#define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(HashSetEntry))
-
-#define hashset_init            pset_init
-#define hashset_init_size       pset_init_size
-#define hashset_destroy         pset_destroy
-#define hashset_insert          pset_insert
-#define hashset_remove          pset_remove
-#define hashset_find            pset_find
-#define hashset_size            pset_size
-#define hashset_iterator_init   pset_iterator_init
-#define hashset_iterator_next   pset_iterator_next
-#define hashset_remove_iterator pset_remove_iterator
-
-#include "hashset.c"
-
-int pset_contains(const pset_t *pset, const ValueType val)
-{
-       return pset_find(pset, val) != NullValue;
-}
-
-#endif
diff --git a/ast.c b/ast.c
index ba88d6a..3e5bf78 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -181,16 +181,11 @@ void print_if_statement(FILE *out, int indent, const if_statement_t *statement)
 }
 
 static
-void print_variable_declaration_statement(FILE *out,
-                     const variable_declaration_statement_t *statement)
+void print_declaration_statement(FILE *out,
+                                 const declaration_statement_t *statement)
 {
-       fprintf(out, "var");
-       if(statement->type != NULL) {
-               fprintf(out, "<");
-               print_type(out, statement->type);
-               fprintf(out, ">");
-       }
-       fprintf(out, " %s", statement->symbol->string);
+       (void) statement;
+       fprintf(out, "*declaration statement*");
 }
 
 void print_statement(FILE *out, int indent, const statement_t *statement)
@@ -219,9 +214,9 @@ void print_statement(FILE *out, int indent, const statement_t *statement)
        case STATEMENT_IF:
                print_if_statement(out, indent, (const if_statement_t*) statement);
                break;
-       case STATEMENT_VARIABLE_DECLARATION:
-               print_variable_declaration_statement(out,
-                       (const variable_declaration_statement_t*) statement);
+       case STATEMENT_DECLARATION:
+               print_declaration_statement(out,
+                                           (const declaration_statement_t*) statement);
                break;
        case STATEMENT_INVALID:
        default:
@@ -232,6 +227,7 @@ void print_statement(FILE *out, int indent, const statement_t *statement)
        fprintf(out, "\n");
 }
 
+#if 0
 static
 void print_method_parameters(FILE *out, const method_parameter_t *parameters,
                              const method_type_t *method_type)
@@ -259,42 +255,20 @@ void print_method_parameters(FILE *out, const method_parameter_t *parameters,
 
        fprintf(out, ")");
 }
+#endif
 
 static
-void print_method(FILE *out, const method_t *method)
+void print_declaration(FILE *out, const declaration_t *declaration)
 {
-       method_type_t *type = method->type;
-
-       fprintf(out, "func ");
-       print_type(out, type->result_type);
-       fprintf(out, " %s", method->symbol->string);
-
-       print_method_parameters(out, method->parameters, type);
-
-       if(method->statement != NULL) {
-               fprintf(out, ":\n");
-               print_statement(out, 0, method->statement);
-       } else {
-               fprintf(out, "\n");
-       }
+       /* TODO */
+       print_type(out, declaration->type);
+       fprintf(out, " %s", declaration->symbol->string);
 }
 
 static
 void print_namespace_entry(FILE *out, const unit_entry_t *entry)
 {
-       switch(entry->type) {
-       case UNIT_ENTRY_METHOD:
-               print_method(out, (const method_t*) entry);
-               break;
-       case UNIT_ENTRY_VARIABLE:
-               /* TODO */
-               fprintf(out, "some namespace entry of type %d\n\n", entry->type);
-               break;
-       case UNIT_ENTRY_INVALID:
-       default:
-               fprintf(out, "invalid namespace entry (%d)\n", entry->type);
-               break;
-       }
+       print_declaration(out, &entry->declaration);
 }
 
 void print_ast(FILE *out, const translation_unit_t *unit)
diff --git a/ast.h b/ast.h
index d316ae0..44ec234 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -20,17 +20,17 @@ typedef struct conditional_expression_t   conditional_expression_t;
 typedef struct expression_list_element_t  expression_list_element_t;
 typedef struct comma_expression_t         comma_expression_t;
 
+typedef struct declaration_t              declaration_t;
+
 typedef struct statement_t                statement_t;
 typedef struct block_statement_t          block_statement_t;
 typedef struct return_statement_t         return_statement_t;
 typedef struct if_statement_t             if_statement_t;
-typedef struct variable_declaration_statement_t
-                                          variable_declaration_statement_t;
+typedef struct declaration_statement_t    declaration_statement_t;
 typedef struct expression_statement_t     expression_statement_t;
 typedef struct goto_statement_t           goto_statement_t;
 typedef struct label_statement_t          label_statement_t;
 
-typedef enum   unit_entry_type_t          unit_entry_type_t;
 typedef struct unit_entry_t               unit_entry_t;
 typedef struct translation_unit_t         translation_unit_t;
 typedef struct method_parameter_t         method_parameter_t;
diff --git a/ast_t.h b/ast_t.h
index 13e8483..fabd945 100644 (file)
--- a/ast_t.h
+++ b/ast_t.h
@@ -42,10 +42,7 @@ struct reference_expression_t {
        expression_t                      expression;
        symbol_t                         *symbol;
        union {
-               variable_declaration_statement_t *variable;
-               method_t                         *method;
-               global_variable_t                *global_variable;
-               method_parameter_t               *method_parameter;
+               declaration_t    *declaration;
        } r;
 };
 
@@ -160,11 +157,36 @@ struct comma_expression_t {
        expression_list_element_t *expressions;
 };
 
+typedef enum {
+       STORAGE_CLASS_NONE,
+       STORAGE_CLASS_TYPEDEF,
+       STORAGE_CLASS_EXTERN,
+       STORAGE_CLASS_STATIC,
+       STORAGE_CLASS_AUTO,
+       STORAGE_CLASS_REGISTER
+} storage_class_t;
+
+struct method_parameter_t {
+       method_parameter_t *next;
+       symbol_t           *symbol;
+       type_t             *type;
+       int                 num;
+};
+
+struct declaration_t {
+       storage_class_t     storage_class;
+       type_t             *type;
+       symbol_t           *symbol;
+       method_parameter_t *parameters;
+       statement_t        *statement;
+       source_position_t   source_position;
+};
+
 typedef enum {
        STATEMENT_INVALID,
        STATEMENT_BLOCK,
        STATEMENT_RETURN,
-       STATEMENT_VARIABLE_DECLARATION,
+       STATEMENT_DECLARATION,
        STATEMENT_IF,
        STATEMENT_EXPRESSION,
        STATEMENT_GOTO,
@@ -187,13 +209,12 @@ struct block_statement_t {
        statement_t *first_statement;
 };
 
-struct variable_declaration_statement_t {
-       statement_t  statement;
-       type_t      *type;
-       symbol_t    *symbol;
+struct declaration_statement_t {
+       statement_t    statement;
+       declaration_t  declaration;
 
-       int          value_number; /**< filled in by semantic phase */
-       int          refs;
+       int            value_number; /**< filled in by semantic phase */
+       int            refs;
 };
 
 struct if_statement_t {
@@ -219,38 +240,9 @@ struct expression_statement_t {
        expression_t *expression;
 };
 
-enum unit_entry_type_t {
-       UNIT_ENTRY_INVALID,
-       UNIT_ENTRY_METHOD,
-       UNIT_ENTRY_VARIABLE,
-};
-
 struct unit_entry_t {
-       unit_entry_type_t  type;
-       unit_entry_t      *next;
-       source_position_t  source_position;
-};
-
-struct method_parameter_t {
-       method_parameter_t *next;
-       symbol_t           *symbol;
-       type_t             *type;
-       int                 num;
-};
-
-struct method_t {
-       unit_entry_t        unit_entry;
-       symbol_t           *symbol;
-       method_type_t      *type;
-       method_parameter_t *parameters;
-
-       statement_t        *statement;
-};
-
-struct global_variable_t {
-       unit_entry_t  unit_entry;
-       symbol_t     *symbol;
-       type_t       *type;
+       declaration_t  declaration;
+       unit_entry_t  *next;
 };
 
 struct translation_unit_t {
diff --git a/lexer.c b/lexer.c
index 406bc7a..ec96a35 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -133,6 +133,61 @@ int replace_trigraph(void)
                newline_code;                          \
        }
 
+#define SYMBOL_CHARS  \
+       case 'a':         \
+       case 'b':         \
+       case 'c':         \
+       case 'd':         \
+       case 'e':         \
+       case 'f':         \
+       case 'g':         \
+       case 'h':         \
+       case 'i':         \
+       case 'j':         \
+       case 'k':         \
+       case 'l':         \
+       case 'm':         \
+       case 'n':         \
+       case 'o':         \
+       case 'p':         \
+       case 'q':         \
+       case 'r':         \
+       case 's':         \
+       case 't':         \
+       case 'u':         \
+       case 'v':         \
+       case 'w':         \
+       case 'x':         \
+       case 'y':         \
+       case 'z':         \
+       case 'A':         \
+       case 'B':         \
+       case 'C':         \
+       case 'D':         \
+       case 'E':         \
+       case 'F':         \
+       case 'G':         \
+       case 'H':         \
+       case 'I':         \
+       case 'J':         \
+       case 'K':         \
+       case 'L':         \
+       case 'M':         \
+       case 'N':         \
+       case 'O':         \
+       case 'P':         \
+       case 'Q':         \
+       case 'R':         \
+       case 'S':         \
+       case 'T':         \
+       case 'U':         \
+       case 'V':         \
+       case 'W':         \
+       case 'X':         \
+       case 'Y':         \
+       case 'Z':         \
+       case '_':
+
 static
 void parse_symbol(token_t *token)
 {
@@ -149,9 +204,7 @@ void parse_symbol(token_t *token)
                        EAT_NEWLINE(break;)
                        goto end_symbol;
 
-               case 'A' ... 'Z':
-               case 'a' ... 'z':
-               case '_':
+               SYMBOL_CHARS
                        obstack_1grow(&symbol_obstack, c);
                        next_char();
                        break;
@@ -181,11 +234,7 @@ end_symbol:
        string = obstack_finish(&symbol_obstack);
        symbol = symbol_table_insert(string);
 
-       if(symbol->ID > 0) {
-               token->type = symbol->ID;
-       } else {
-               token->type = T_IDENTIFIER;
-       }
+       token->type     = symbol->ID;
        token->v.symbol = symbol;
 
        if(symbol->string != string) {
@@ -307,7 +356,15 @@ int parse_escape_sequence()
                case 'x': /* TODO parse hex number ... */
                        parse_error("hex escape sequences not implemented yet");
                        return EOF;
-               case 0 ... 8: /* TODO parse octal number ... */
+               case 0:
+               case 1:
+               case 2:
+               case 3:
+               case 4:
+               case 5:
+               case 6:
+               case 7:
+                       /* TODO parse octal number ... */
                        parse_error("octal escape sequences not implemented yet");
                        return EOF;
                case '?':
@@ -555,6 +612,14 @@ void skip_line_comment(void)
 static
 void lexer_next_preprocessing_token(token_t *token);
 
+static token_t pp_token;
+
+static inline
+void next_pp_token(void)
+{
+       lexer_next_preprocessing_token(&pp_token);
+}
+
 static
 void eat_until_newline(void)
 {
@@ -599,8 +664,30 @@ void endif_directive(void)
 }
 
 static
-void found_preprocessor_identifier(symbol_t *symbol)
+void parse_line_directive(void)
 {
+       if(pp_token.type != T_INTEGER) {
+               parse_error("expected integer");
+       } else {
+               source_position.linenr = pp_token.v.intvalue - 1;
+               next_pp_token();
+       }
+       if(pp_token.type == T_STRING_LITERAL) {
+               source_position.input_name = pp_token.v.string;
+               next_pp_token();
+       }
+
+       while(pp_token.type != T_EOF && pp_token.type != '\n') {
+               next_pp_token();
+       }
+}
+
+static
+void parse_preprocessor_identifier(void)
+{
+       assert(pp_token.type == T_IDENTIFIER);
+       symbol_t *symbol = pp_token.v.symbol;
+
        switch(symbol->pp_ID) {
        case TP_include:
                printf("include - enable header name parsing!\n");
@@ -617,11 +704,14 @@ void found_preprocessor_identifier(symbol_t *symbol)
        case TP_endif:
                endif_directive();
                break;
+       case TP_line:
+               next_pp_token();
+               parse_line_directive();
+               break;
        case TP_if:
        case TP_else:
        case TP_elif:
        case TP_undef:
-       case TP_line:
        case TP_error:
                error_directive();
                break;
@@ -633,15 +723,18 @@ void found_preprocessor_identifier(symbol_t *symbol)
 static
 void parse_preprocessor_directive(token_t *result_token)
 {
-       token_t temptoken;
+       next_pp_token();
 
-       (void) result_token;
-       lexer_next_preprocessing_token(&temptoken);
-       switch(temptoken.type) {
+       switch(pp_token.type) {
        case T_IDENTIFIER:
-               found_preprocessor_identifier(temptoken.v.symbol);
+               parse_preprocessor_identifier();
+               break;
+       case T_INTEGER:
+               parse_line_directive();
                break;
        }
+
+       lexer_next_token(result_token);
 }
 
 #define MAYBE_PROLOG                                       \
@@ -765,13 +858,20 @@ void lexer_next_preprocessing_token(token_t *token)
                        return;
                )
 
-               case 'A' ... 'Z':
-               case 'a' ... 'z':
-               case '_':
+               SYMBOL_CHARS
                        parse_symbol(token);
                        return;
 
-               case '0' ... '9':
+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               case '4':
+               case '5':
+               case '6':
+               case '7':
+               case '8':
+               case '9':
                        parse_number(token);
                        return;
 
index 4d67fc3..94c822b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -3,6 +3,7 @@
 #include <assert.h>
 #include <stdarg.h>
 
+#include "parser.h"
 #include "lexer_t.h"
 #include "token_t.h"
 #include "type_t.h"
 #include "ast_t.h"
 #include "adt/bitfiddle.h"
 #include "adt/error.h"
+#include "adt/array.h"
 
 #define PRINT_TOKENS
 
-static token_t token;
+struct environment_entry_t {
+       symbol_t            *symbol;
+       environment_entry_t *old_entry;
+       declaration_t       *declaration;
+       unsigned short       old_symbol_ID;
+};
+
+static token_t               token;
+static struct obstack        environment_obstack;
+static environment_entry_t **environment_stack = NULL;
+static translation_unit_t   *translation_unit  = NULL;
+static block_statement_t    *context           = NULL;
+
+static
+statement_t *parse_compound_statement(void);
+static
+statement_t *parse_statement(void);
+
+static
+expression_t *parse_sub_expression(unsigned precedence);
+static
+expression_t *parse_expression(void);
 
 static inline
 void *allocate_ast_zero(size_t size)
@@ -23,6 +46,92 @@ void *allocate_ast_zero(size_t size)
        return res;
 }
 
+static inline
+void *allocate_type_zero(size_t size)
+{
+       void *res = obstack_alloc(type_obst, size);
+       memset(res, 0, size);
+       return res;
+}
+
+/**
+ * pushs an environment_entry on the environment stack and links the
+ * corresponding symbol to the new entry
+ */
+static inline
+environment_entry_t *environment_push(symbol_t *symbol)
+{
+       environment_entry_t *entry
+               = obstack_alloc(&environment_obstack, sizeof(entry[0]));
+       memset(entry, 0, sizeof(entry[0]));
+
+       int top = ARR_LEN(environment_stack);
+       ARR_RESIZE(environment_stack, top + 1);
+       environment_stack[top] = entry;
+
+       entry->old_entry = symbol->thing;
+       entry->symbol    = symbol;
+       symbol->thing    = entry;
+
+       return entry;
+}
+
+/**
+ * pops symbols from the environment stack until @p new_top is the top element
+ */
+static inline
+void environment_pop_to(size_t new_top)
+{
+       environment_entry_t *entry = NULL;
+       size_t top = ARR_LEN(environment_stack);
+       size_t i;
+
+       if(new_top == top)
+               return;
+
+       assert(new_top < top);
+       i = top;
+       do {
+               entry = environment_stack[i - 1];
+
+               symbol_t *symbol = entry->symbol;
+
+#if 0
+               if(entry->type == ENTRY_LOCAL_VARIABLE
+                               && entry->e.variable->refs == 0) {
+                       variable_declaration_statement_t *variable = entry->e.variable;
+                       print_warning_prefix(env, variable->statement.source_position);
+                       fprintf(stderr, "variable '%s' was declared but never read\n",
+                               symbol->string);
+               }
+#endif
+
+               if(entry->declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
+                       fprintf(stderr, "pop typename '%s'\n", entry->symbol->string);
+                       symbol->ID = entry->old_symbol_ID;
+               }
+
+               assert(symbol->thing == entry);
+               symbol->thing = entry->old_entry;
+
+               --i;
+       } while(i != new_top);
+       obstack_free(&environment_obstack, entry);
+
+       ARR_SHRINKLEN(environment_stack, (int) new_top);
+}
+
+/**
+ * returns the top element of the environment stack
+ */
+static inline
+size_t environment_top()
+{
+       return ARR_LEN(environment_stack);
+}
+
+
+
 static inline
 void next_token(void)
 {
@@ -113,6 +222,68 @@ void eat_until_semi(void)
     }                                              \
     next_token();
 
+static expression_t *parse_constant_expression(void)
+{
+       /* TODO: not correct yet */
+       return parse_expression();
+}
+
+static compound_entry_t *parse_compound_type_entries(void);
+
+typedef struct declaration_specifiers_t  declaration_specifiers_t;
+struct declaration_specifiers_t {
+       storage_class_t  storage_class;
+       type_t          *type;
+};
+
+static type_t *parse_struct_specifier(void)
+{
+       eat(T_struct);
+
+       compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
+       struct_type->type.type       = TYPE_COMPOUND_STRUCT;
+       struct_type->source_position = source_position;
+
+       if(token.type == T_IDENTIFIER) {
+               /* TODO */
+               next_token();
+               if(token.type == '{') {
+                       parse_compound_type_entries();
+               }
+       } else if(token.type == '{') {
+               parse_compound_type_entries();
+       } else {
+               parse_error_expected("problem while parsing struct type specifiers",
+                                    T_IDENTIFIER, '{');
+               return NULL;
+       }
+
+       return (type_t*) struct_type;
+}
+
+static type_t *parse_union_specifier(void)
+{
+       eat(T_union);
+
+       compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
+       union_type->type.type       = TYPE_COMPOUND_UNION;
+       union_type->source_position = source_position;
+
+       if(token.type == T_IDENTIFIER) {
+               /* TODO */
+               next_token();
+               if(token.type == '{') {
+                       parse_compound_type_entries();
+               }
+       } else if(token.type == '{') {
+               parse_compound_type_entries();
+       } else {
+               parse_error_expected("problem while parsing union type specifiers",
+                                    T_IDENTIFIER, '{');
+       }
+
+       return (type_t*) union_type;
+}
 
 typedef enum {
        SPECIFIER_SIGNED    = 1 << 0,
@@ -134,28 +305,61 @@ typedef enum {
 #endif
 } specifiers_t;
 
-typedef enum {
-       STORAGE_CLASS_NONE,
-       STORAGE_CLASS_TYPEDEF,
-       STORAGE_CLASS_EXTERN,
-       STORAGE_CLASS_STATIC,
-       STORAGE_CLASS_AUTO,
-       STORAGE_CLASS_REGISTER
-} storage_class_t;
+#define STORAGE_CLASSES     \
+       case T_typedef:         \
+       case T_extern:          \
+       case T_static:          \
+       case T_auto:            \
+       case T_register:
+
+#define TYPE_QUALIFIERS     \
+       case T_const:           \
+       case T_restrict:        \
+       case T_volatile:        \
+       case T_inline:          \
+       case T___extension__:   \
+       case T___attribute__:
 
-typedef struct declaration_specifiers_t  declaration_specifiers_t;
-struct declaration_specifiers_t {
-       storage_class_t  storage_class;
-       type_t          *type;
-};
+#ifdef PROVIDE_COMPLEX
+#define COMPLEX_SPECIFIERS  \
+       case T__Complex:
+#else
+#define COMPLEX_SPECIFIERS
+#endif
+
+#ifdef PROVIDE_IMAGINARY
+#define IMAGINARY_SPECIFIERS \
+       case T__Imaginary:
+#else
+#define IMAGINARY_SPECIFIERS
+#endif
+
+#define TYPE_SPECIFIERS     \
+       case T_TYPENAME:        \
+       case T_void:            \
+       case T_char:            \
+       case T_short:           \
+       case T_int:             \
+       case T_long:            \
+       case T_float:           \
+       case T_double:          \
+       case T_signed:          \
+       case T_unsigned:        \
+       case T__Bool:           \
+       case T_struct:          \
+       case T_union:           \
+       case T_enum:            \
+       case T___quad_t:        \
+       case T___u_quad_t:      \
+       COMPLEX_SPECIFIERS      \
+       IMAGINARY_SPECIFIERS
 
 static
 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
 {
-       type_type_t        type_type       = TYPE_INVALID;
-       atomic_type_type_t atomic_type     = ATOMIC_TYPE_INVALID;
-       unsigned           type_qualifiers = 0;
-       unsigned           type_specifiers = 0;
+       type_t             *type            = NULL;
+       unsigned            type_qualifiers = 0;
+       unsigned            type_specifiers = 0;
 
        while(1) {
                switch(token.type) {
@@ -189,6 +393,16 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
                MATCH_TYPE_QUALIFIER(T_inline,   TYPE_QUALIFIER_INLINE);
 
+               case T___extension__:
+                       /* TODO */
+                       next_token();
+                       break;
+
+               case T___attribute__:
+                       fprintf(stderr, "TODO: __attribute__ not handled yet\n");
+                       next_token();
+                       break;
+
                /* type specifiers */
 #define MATCH_SPECIFIER(token, specifier, name)                         \
                case token:                                                     \
@@ -218,7 +432,7 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                case T_long:
                        next_token();
                        if(type_specifiers & SPECIFIER_LONG_LONG) {
-                               parse_error("too many long type specifiers given");
+                               parse_error("multiple type specifiers given");
                        } else if(type_specifiers & SPECIFIER_LONG) {
                                type_specifiers |= SPECIFIER_LONG_LONG;
                        } else {
@@ -226,12 +440,39 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        }
                        break;
 
+               case T___quad_t:
+                       next_token();
+                       if(type_specifiers & SPECIFIER_LONG_LONG ||
+                                       type_specifiers & SPECIFIER_LONG) {
+                               parse_error("multiple type specifiers given");
+                       } else {
+                               type_specifiers |= specifier;
+                       }
+                       break;
+
+                       type_specifiers
+
                case T_struct:
+                       type = parse_struct_specifier();
+                       break;
+               case T_union:
+                       type = parse_union_specifier();
+                       break;
                case T_enum:
                        /* TODO */
                        assert(0);
                        break;
 
+               case T_TYPENAME:
+                       if(type != NULL || type_specifiers != 0) {
+                               goto finish_specifiers;
+                       }
+
+                       type = token.v.symbol->thing->declaration->type;
+                       assert(type != NULL);
+                       next_token();
+                       break;
+
                /* function specifier */
                default:
                        goto finish_specifiers;
@@ -239,7 +480,10 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
        }
 
 finish_specifiers:
-       if(type_type == TYPE_INVALID) {
+
+       if(type == NULL) {
+               atomic_type_type_t atomic_type;
+
                /* match valid basic types */
                switch(type_specifiers) {
                case SPECIFIER_VOID:
@@ -342,27 +586,29 @@ finish_specifiers:
                        atomic_type = ATOMIC_TYPE_INVALID;
                }
 
-               atomic_type_t *type = obstack_alloc(type_obst, sizeof(type[0]));
-               memset(type, 0, sizeof(type[0]));
-               type->type.type       = TYPE_ATOMIC;
-               type->type.qualifiers = type_qualifiers;
-               type->atype           = atomic_type;
-
-               type_t *result = typehash_insert((type_t*) type);
-               if(result != (type_t*) type) {
-                       obstack_free(type_obst, type);
-               }
-
-               specifiers->type = result;
+               atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
+               atype->type.type     = TYPE_ATOMIC;
+               atype->atype         = atomic_type;
 
-               fprintf(stderr, "Specifiers type: ");
-               print_type(stderr, result);
-               fprintf(stderr, "\n");
+               type = (type_t*) atype;
        } else {
                if(type_specifiers != 0) {
                        parse_error("multiple datatypes in declaration");
                }
        }
+
+       type->qualifiers = type_qualifiers;
+
+       type_t *result = typehash_insert(type);
+       if(result != (type_t*) type) {
+               obstack_free(type_obst, type);
+       }
+
+       specifiers->type = result;
+
+       fprintf(stderr, "Specifiers type: ");
+       print_type(stderr, result);
+       fprintf(stderr, "\n");
 }
 
 static
@@ -385,17 +631,15 @@ unsigned parse_type_qualifiers()
 }
 
 static
-void parse_declarator(const declaration_specifiers_t *specifiers)
+void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
+                      type_t *type)
 {
-       type_t *type = specifiers->type;
-
        while(token.type == '*') {
                /* pointer */
                next_token();
 
                pointer_type_t *pointer_type
-                       = obstack_alloc(type_obst, sizeof(pointer_type[0]));
-               memset(pointer_type, 0, sizeof(pointer_type[0]));
+                       = allocate_type_zero(sizeof(pointer_type[0]));
                pointer_type->type.type = TYPE_POINTER;
                pointer_type->points_to = type;
 
@@ -408,17 +652,20 @@ void parse_declarator(const declaration_specifiers_t *specifiers)
 
                type = result;
        }
+       declaration->storage_class = storage_class;
+       declaration->type          = type;
 
        switch(token.type) {
+       case T_TYPENAME:
        case T_IDENTIFIER:
-               (void) token.v.symbol;
+               declaration->symbol = token.v.symbol;
                next_token();
                break;
        case '(':
                next_token();
-               parse_declarator(specifiers);
+               parse_declarator(declaration, storage_class, type);
                expect_void(')');
-               return;
+               break;
        default:
                parse_error("problem while parsing declarator");
        }
@@ -442,15 +689,90 @@ void parse_declarator(const declaration_specifiers_t *specifiers)
        fprintf(stderr, "Declarator type: ");
        print_type(stderr, type);
        fprintf(stderr, "\n");
+
+       symbol_t *symbol = declaration->symbol;
+
+       environment_entry_t *entry = environment_push(symbol);
+       entry->declaration         = declaration;
+       entry->old_symbol_ID       = symbol->ID;
+
+       if(storage_class == STORAGE_CLASS_TYPEDEF) {
+               symbol->ID       = T_TYPENAME;
+       } else {
+               symbol->ID       = T_IDENTIFIER;
+       }
 }
 
+static
 void parse_init_declarators(const declaration_specifiers_t *specifiers)
 {
-       parse_declarator(specifiers);
-       if(token.type == '=') {
+       while(1) {
+               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
+
+               parse_declarator(declaration, specifiers->storage_class,
+                                specifiers->type);
+               if(token.type == '=') {
+                       next_token();
+                       // parse_initializer TODO
+               } else if(token.type == '{') {
+                       parse_compound_statement();
+                       return;
+               }
+
+               if(token.type != ',')
+                       break;
+               next_token();
+       }
+       expect_void(';');
+}
+
+static
+void parse_struct_declarators(const declaration_specifiers_t *specifiers)
+{
+       while(1) {
+               declaration_t declaration;
+               compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
+
+               if(token.type == ':') {
+                       next_token();
+                       parse_constant_expression();
+                       /* TODO */
+               } else {
+                       parse_declarator(&declaration, specifiers->storage_class,
+                                        specifiers->type);
+
+                       if(token.type == ':') {
+                               next_token();
+                               parse_constant_expression();
+                               /* TODO */
+                       }
+               }
+
+               if(token.type != ',')
+                       break;
                next_token();
-               //parse_initialize();
        }
+       expect_void(';');
+}
+
+static compound_entry_t *parse_compound_type_entries(void)
+{
+       eat('{');
+
+       compound_entry_t *entries = NULL;
+
+       while(token.type != '}' && token.type != T_EOF) {
+               declaration_specifiers_t specifiers;
+               memset(&specifiers, 0, sizeof(specifiers));
+               /* TODO not correct as this allows storage class stuff... but only
+                * specifiers and qualifiers sould be allowed here */
+               parse_declaration_specifiers(&specifiers);
+
+               parse_struct_declarators(&specifiers);
+       }
+       next_token();
+
+       return entries;
 }
 
 void parse_declaration(void)
@@ -459,16 +781,15 @@ void parse_declaration(void)
        memset(&specifiers, 0, sizeof(specifiers));
        parse_declaration_specifiers(&specifiers);
 
+       if(token.type == ';') {
+               next_token();
+               return;
+       }
        parse_init_declarators(&specifiers);
 }
 
 
 
-static
-expression_t *parse_sub_expression(unsigned precedence);
-static
-expression_t *parse_expression(void);
-
 typedef expression_t* (*parse_expression_function) (unsigned precedence);
 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
                                                           expression_t *left);
@@ -677,14 +998,14 @@ expression_t *parse_##unexpression_type(unsigned precedence)              \
        return (expression_t*) unary_expression;                              \
 }
 
-CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE);
-CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS);
-CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT);
-CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE);
-CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS);
-CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE);
-CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT);
-CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT);
+CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
+CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
+CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
+CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
+CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
+CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
+CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_PREFIX_INCREMENT)
+CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
 
 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
 static                                                                        \
@@ -703,8 +1024,8 @@ expression_t *parse_##unexpression_type(unsigned precedence,                  \
        return (expression_t*) unary_expression;                                  \
 }
 
-CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT);
-CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT);
+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,   UNEXPR_POSTFIX_INCREMENT)
+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
 
 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type)    \
 static                                                           \
@@ -725,22 +1046,22 @@ expression_t *parse_##binexpression_type(unsigned precedence,    \
        return (expression_t*) binexpr;                              \
 }
 
-CREATE_BINEXPR_PARSER('*', BINEXPR_MUL);
-CREATE_BINEXPR_PARSER('/', BINEXPR_DIV);
-CREATE_BINEXPR_PARSER('+', BINEXPR_ADD);
-CREATE_BINEXPR_PARSER('-', BINEXPR_SUB);
-CREATE_BINEXPR_PARSER('<', BINEXPR_LESS);
-CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER);
-CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN);
-CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL);
-CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL);
-CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL);
-CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL);
-CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND);
-CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR);
-CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR);
-CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT);
-CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT);
+CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
+CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
+CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
+CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
+CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
+CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
+CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
+CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
+CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
+CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
+CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
+CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
+CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
+CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
+CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
+CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
 
 static
 expression_t *parse_sub_expression(unsigned precedence)
@@ -867,12 +1188,6 @@ void init_expression_parsers(void)
 }
 
 
-static
-statement_t *parse_compound_statement(void);
-
-static
-statement_t *parse_statement(void);
-
 static
 statement_t *parse_case_statement(void)
 {
@@ -1019,6 +1334,13 @@ statement_t *parse_return(void)
        return NULL;
 }
 
+static
+statement_t *parse_declaration_statement(void)
+{
+       parse_declaration();
+       return NULL;
+}
+
 static
 statement_t *parse_statement(void)
 {
@@ -1081,6 +1403,12 @@ statement_t *parse_statement(void)
        case ';':
                statement = NULL;
                break;
+
+       STORAGE_CLASSES
+       TYPE_QUALIFIERS
+       TYPE_SPECIFIERS
+               statement = parse_declaration_statement();
+               break;
        }
 
        return statement;
@@ -1089,48 +1417,50 @@ statement_t *parse_statement(void)
 static
 statement_t *parse_compound_statement(void)
 {
-       expect('{');
+       eat('{');
+
+       int top = environment_top();
 
        while(token.type != '}') {
                parse_statement();
        }
+
+       environment_pop_to(top);
+
        next_token();
 
        return NULL;
 }
 
 static
-void parse_translation_unit(void)
+translation_unit_t *parse_translation_unit(void)
 {
-       /*
-       declaration_specifiers_t specifiers;
-       memset(&specifiers, 0, sizeof(specifiers));
-       parse_declaration_specifiers(&specifiers);
-       */
+       translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
 
-       while(token.type != T_EOF) {
-               if(token.type == '{') {
-                       next_token();
-                       continue;
-               }
+       assert(translation_unit == NULL);
+       assert(context == NULL);
+       translation_unit = unit;
 
+       while(token.type != T_EOF) {
                parse_declaration();
-               /* multiple declarations? */
-
-               if(token.type == '{') {
-                       parse_compound_statement();
-               } else if(token.type == ';') {
-                       next_token();
-               } else {
-                       parse_error_expected("while parsing declarations", '{', ';', 0);
-               }
        }
+
+       translation_unit = NULL;
+       return unit;
 }
 
-void parse(void)
+translation_unit_t *parse(void)
 {
+       obstack_init(&environment_obstack);
+       environment_stack = NEW_ARR_F(environment_entry_t*, 0);
+
        next_token();
-       parse_translation_unit();
+       translation_unit_t *unit = parse_translation_unit();
+
+       DEL_ARR_F(environment_stack);
+       obstack_free(&environment_obstack, NULL);
+
+       return unit;
 }
 
 void init_parser(void)
index d9df449..92a3714 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -3,6 +3,8 @@
 
 #include "ast.h"
 
+typedef struct environment_entry_t environment_entry_t;
+
 void init_parser(void);
 void exit_parser(void);
 
index 895bfea..11efafb 100644 (file)
--- a/symbol.h
+++ b/symbol.h
@@ -1,12 +1,15 @@
 #ifndef SYMBOL_H
 #define SYMBOL_H
 
+#include "parser.h"
+
 typedef struct symbol_t symbol_t;
 
 struct symbol_t {
        const char          *string;
        unsigned short       ID;
        unsigned short       pp_ID;
+       environment_entry_t *thing;
 };
 
 #endif
index 03cbdd5..ca65de6 100644 (file)
@@ -1,6 +1,7 @@
 #include <config.h>
 
 #include "symbol_table_t.h"
+#include "token_t.h"
 #include "adt/hash_string.h"
 #include "adt/obst.h"
 
@@ -9,9 +10,10 @@ struct obstack symbol_obstack;
 static inline
 void init_symbol_table_entry(symbol_t *entry, const char *string)
 {
-       entry->ID     = 0;
-       entry->pp_ID  = 0;
        entry->string = string;
+       entry->ID     = T_IDENTIFIER;
+       entry->pp_ID  = 0;
+       entry->thing  = NULL;
 }
 
 #define HashSet                    symbol_table_t
diff --git a/token.c b/token.c
index 1496f35..f9b5336 100644 (file)
--- a/token.c
+++ b/token.c
@@ -81,6 +81,9 @@ void print_token(FILE *f, const token_t *token)
        case T_IDENTIFIER:
                fprintf(f, "symbol '%s'", token->v.symbol->string);
                break;
+       case T_TYPENAME:
+               fprintf(f, "typename '%s'", token->v.symbol->string);
+               break;
        case T_INTEGER:
                fprintf(f, "integer number %d", token->v.intvalue);
                break;
index d5626d4..fc22fad 100644 (file)
@@ -45,6 +45,10 @@ S(while)
 S(_Bool)
 S(_Complex)
 S(_Imaginary)
+S(__extension__)
+S(__attribute__)
+S(__u_quad_t)
+S(__quad_t)
 #undef S
 
 T(SELECT,                   "->",)
diff --git a/type.c b/type.c
index 9ac9f2e..fade11f 100644 (file)
--- a/type.c
+++ b/type.c
@@ -115,7 +115,9 @@ void print_type(FILE *out, const type_t *type)
        case TYPE_COMPOUND_STRUCT:
        case TYPE_COMPOUND_UNION:
                print_type_qualifiers(out, type);
-               fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
+               if(((const compound_type_t*) type)->symbol != NULL) {
+                       fprintf(out, "%s", ((const compound_type_t*) type)->symbol->string);
+               }
                return;
        case TYPE_METHOD:
                print_method_type(out, (const method_type_t*) type);
index b3ae801..312b8cf 100644 (file)
@@ -18,7 +18,7 @@
 static
 unsigned hash_ptr(const void *ptr)
 {
-       unsigned ptr_int = (ptr - NULL);
+       unsigned ptr_int = ((char*) ptr - (char*) NULL);
        return ptr_int >> 3;
 }