support for writing functions and function types in fluffy export
[cparser] / parser.c
index 7719371..9cae963 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4,7 +4,7 @@
 #include <stdarg.h>
 
 #include "parser.h"
-#include "lexer_t.h"
+#include "lexer.h"
 #include "token_t.h"
 #include "type_t.h"
 #include "type_hash.h"
 #include "adt/error.h"
 #include "adt/array.h"
 
-#define PRINT_TOKENS
+//#define PRINT_TOKENS
+#define MAX_LOOKAHEAD 2
 
 struct environment_entry_t {
-       symbol_t            *symbol;
-       environment_entry_t *old_entry;
-       declaration_t       *declaration;
-       unsigned short       old_symbol_ID;
+       symbol_t      *symbol;
+       declaration_t *old_declaration;
+       const void    *old_context;
 };
 
 static token_t               token;
+static token_t               lookahead_buffer[MAX_LOOKAHEAD];
+static int                   lookahead_bufpos;
 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 context_t            *context           = NULL;
+static declaration_t        *last_declaration  = NULL;
 
 static
 statement_t *parse_compound_statement(void);
@@ -54,73 +56,6 @@ void *allocate_type_zero(size_t 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
  */
@@ -135,7 +70,11 @@ size_t environment_top()
 static inline
 void next_token(void)
 {
-       lexer_next_token(&token);
+       token                              = lookahead_buffer[lookahead_bufpos];
+       lookahead_buffer[lookahead_bufpos] = lexer_token;
+       lexer_next_token();
+
+       lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
 
 #ifdef PRINT_TOKENS
        print_token(stderr, &token);
@@ -143,6 +82,14 @@ void next_token(void)
 #endif
 }
 
+static inline
+const token_t *la(int num)
+{
+       assert(num > 0 && num <= MAX_LOOKAHEAD);
+       int pos = (num-1) % MAX_LOOKAHEAD;
+       return & lookahead_buffer[pos];
+}
+
 static inline
 void eat(token_type_t type)
 {
@@ -150,7 +97,7 @@ void eat(token_type_t type)
        next_token();
 }
 
-void parser_print_error_prefix(void)
+void parser_print_error_prefix_pos(const source_position_t source_position)
 {
     fputs(source_position.input_name, stderr);
     fputc(':', stderr);
@@ -158,6 +105,11 @@ void parser_print_error_prefix(void)
     fputs(": error: ", stderr);
 }
 
+void parser_print_error_prefix(void)
+{
+       parser_print_error_prefix_pos(token.source_position);
+}
+
 static
 void parse_error(const char *message)
 {
@@ -222,6 +174,92 @@ void eat_until(int token_type)
     }                                              \
     next_token();
 
+static void set_context(context_t *new_context)
+{
+       context = new_context;
+
+       declaration_t *declaration = new_context->declarations;
+       if(declaration != NULL) {
+               while(1) {
+                       if(declaration->next == NULL)
+                               break;
+                       declaration = declaration->next;
+               }
+       }
+
+       last_declaration = declaration;
+}
+
+/**
+ * pushs an environment_entry on the environment stack and links the
+ * corresponding symbol to the new entry
+ */
+static inline
+void environment_push(declaration_t *declaration, const void *context)
+{
+       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;
+
+       assert(declaration->source_position.input_name != NULL);
+
+       symbol_t *symbol = declaration->symbol;
+       assert(declaration != symbol->declaration);
+
+       if(symbol->context == context) {
+               if(symbol->declaration != NULL) {
+                       assert(symbol->declaration != NULL);
+                       parser_print_error_prefix_pos(declaration->source_position);
+                       fprintf(stderr, "multiple definitions for symbol '%s'.\n",
+                                       symbol->string);
+                       parser_print_error_prefix_pos(symbol->declaration->source_position);
+                       fprintf(stderr, "this is the location of the previous declaration.\n");
+               }
+       }
+
+       entry->old_declaration = symbol->declaration;
+       entry->old_context     = symbol->context;
+       entry->symbol          = symbol;
+       symbol->declaration    = declaration;
+       symbol->context        = context;
+}
+
+/**
+ * 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;
+
+               symbol->declaration = entry->old_declaration;
+               symbol->context     = entry->old_context;
+
+               --i;
+       } while(i != new_top);
+       obstack_free(&environment_obstack, entry);
+
+       ARR_SHRINKLEN(environment_stack, (int) new_top);
+}
+
+
+
 static expression_t *parse_constant_expression(void)
 {
        /* TODO: not correct yet */
@@ -234,9 +272,11 @@ static expression_t *parse_assignment_expression(void)
        return parse_expression();
 }
 
-static compound_entry_t *parse_compound_type_entries(void);
+static void parse_compound_type_entries(void);
 static void parse_declarator(declaration_t *declaration,
                              storage_class_t storage_class, type_t *type);
+static void maybe_push_declaration(declaration_t *declaration);
+static void record_declaration(declaration_t *declaration);
 
 typedef struct declaration_specifiers_t  declaration_specifiers_t;
 struct declaration_specifiers_t {
@@ -250,10 +290,13 @@ static type_t *parse_struct_specifier(void)
 
        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;
+       struct_type->source_position = token.source_position;
+
+       int         top          = environment_top();
+       context_t  *last_context = context;
+       set_context(&struct_type->context);
 
-       if(token.type == T_IDENTIFIER || token.type == T_TYPENAME) {
-               /* TODO */
+       if(token.type == T_IDENTIFIER) {
                next_token();
                if(token.type == '{') {
                        parse_compound_type_entries();
@@ -263,9 +306,13 @@ static type_t *parse_struct_specifier(void)
        } else {
                parse_error_expected("problem while parsing struct type specifiers",
                                     T_IDENTIFIER, '{', 0);
-               return NULL;
+               struct_type = NULL;
        }
 
+       assert(context == &struct_type->context);
+       set_context(last_context);
+       environment_pop_to(top);
+
        return (type_t*) struct_type;
 }
 
@@ -275,7 +322,11 @@ static type_t *parse_union_specifier(void)
 
        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;
+       union_type->source_position = token.source_position;
+
+       int         top          = environment_top();
+       context_t  *last_context = context;
+       set_context(&union_type->context);
 
        if(token.type == T_IDENTIFIER) {
                union_type->symbol = token.v.symbol;
@@ -288,8 +339,13 @@ static type_t *parse_union_specifier(void)
        } else {
                parse_error_expected("problem while parsing union type specifiers",
                                     T_IDENTIFIER, '{');
+               union_type = NULL;
        }
 
+       assert(context == &union_type->context);
+       set_context(last_context);
+       environment_pop_to(top);
+
        return (type_t*) union_type;
 }
 
@@ -330,7 +386,7 @@ static type_t *parse_enum_specifier(void)
 
        enum_type_t *enum_type     = allocate_type_zero(sizeof(enum_type[0]));
        enum_type->type.type       = TYPE_ENUM;
-       enum_type->source_position = source_position;
+       enum_type->source_position = token.source_position;
 
        if(token.type == T_IDENTIFIER) {
                enum_type->symbol = token.v.symbol;
@@ -397,7 +453,6 @@ typedef enum {
 #endif
 
 #define TYPE_SPECIFIERS     \
-       case T_TYPENAME:        \
        case T_void:            \
        case T_char:            \
        case T_short:           \
@@ -437,9 +492,11 @@ type_t *create_builtin_type(symbol_t *symbol)
 static
 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
 {
-       type_t             *type            = NULL;
-       unsigned            type_qualifiers = 0;
-       unsigned            type_specifiers = 0;
+       declaration_t *declaration;
+       type_t        *type            = NULL;
+       unsigned       type_qualifiers = 0;
+       unsigned       type_specifiers = 0;
+       int            newtype         = 0;
 
        while(1) {
                switch(token.type) {
@@ -515,6 +572,7 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        }
                        break;
 
+               /* TODO: if type != NULL for the following rules issue an error */
                case T_struct:
                        type = parse_struct_specifier();
                        break;
@@ -529,12 +587,14 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        next_token();
                        break;
 
-               case T_TYPENAME:
-                       if(type != NULL || type_specifiers != 0) {
+               case T_IDENTIFIER:
+                       declaration = token.v.symbol->declaration;
+                       if(declaration == NULL ||
+                                       declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
                                goto finish_specifiers;
                        }
 
-                       type = token.v.symbol->thing->declaration->type;
+                       type = declaration->type;
                        assert(type != NULL);
                        next_token();
                        break;
@@ -657,6 +717,7 @@ finish_specifiers:
                atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
                atype->type.type     = TYPE_ATOMIC;
                atype->atype         = atomic_type;
+               newtype              = 1;
 
                type = (type_t*) atype;
        } else {
@@ -668,15 +729,11 @@ finish_specifiers:
        type->qualifiers = type_qualifiers;
 
        type_t *result = typehash_insert(type);
-       if(result != (type_t*) type) {
+       if(newtype && result != (type_t*) type) {
                obstack_free(type_obst, type);
        }
 
        specifiers->type = result;
-
-       fprintf(stderr, "Specifiers type: ");
-       print_type(stderr, result);
-       fprintf(stderr, "\n");
 }
 
 static
@@ -753,12 +810,12 @@ void parse_parameter()
        parse_declaration_specifiers(&specifiers);
        specifiers.type = parse_pointer(specifiers.type);
 
-       if(token.type == '(' || token.type == T_IDENTIFIER
-                       || token.type == T_TYPENAME) {
-               declaration_t declaration;
-               memset(&declaration, 0, sizeof(declaration));
-               parse_declarator(&declaration, specifiers.storage_class,
+       if(token.type == '(' || token.type == T_IDENTIFIER) {
+               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
+               parse_declarator(declaration, specifiers.storage_class,
                                 specifiers.type);
+               maybe_push_declaration(declaration);
+               record_declaration(declaration);
        }
 }
 
@@ -766,13 +823,19 @@ static
 void parse_parameters()
 {
        if(token.type == T_IDENTIFIER) {
-               parse_identifier_list();
-               return;
+               symbol_t      *symbol      = token.v.symbol;
+               declaration_t *declaration = symbol->declaration;
+               if(declaration == NULL
+                               || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
+                       parse_identifier_list();
+                       return;
+               }
        }
 
        while(1) {
                switch(token.type) {
                case T_DOTDOTDOT:
+               case T_IDENTIFIER:
                DECLARATION_START
                        parse_parameter();
                        break;
@@ -790,7 +853,6 @@ void parse_attributes(void)
 {
        while(token.type == T___attribute__) {
                next_token();
-               fprintf(stderr, "TODO: __attribute__ not handled yet\n");
 
                expect_void('(');
                int depth = 1;
@@ -823,9 +885,9 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
        declaration->type          = type;
 
        switch(token.type) {
-       case T_TYPENAME:
        case T_IDENTIFIER:
-               declaration->symbol = token.v.symbol;
+               declaration->symbol          = token.v.symbol;
+               declaration->source_position = token.source_position;
                next_token();
                break;
        case '(':
@@ -834,8 +896,8 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                expect_void(')');
                break;
        default:
-               parse_error_expected("problem while parsing declarator", T_TYPENAME,
-                                    T_IDENTIFIER, '(', 0);
+               parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
+                                    '(', 0);
        }
 
        while(1) {
@@ -843,8 +905,16 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                case '(':
                        next_token();
 
+                       int         top          = environment_top();
+                       context_t  *last_context = context;
+                       set_context(&declaration->context);
+
                        parse_parameters();
 
+                       assert(context == &declaration->context);
+                       set_context(last_context);
+                       environment_pop_to(top);
+
                        expect_void(')');
                        break;
                case '[':
@@ -861,7 +931,7 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                                }
                        }
 
-                       if(token.type == '*' /* TODO: && lookahead == ']' */) {
+                       if(token.type == '*' && la(1)->type == ']') {
                                next_token();
                        } else if(token.type != ']') {
                                parse_assignment_expression();
@@ -876,24 +946,26 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
 
 declarator_finished:
        parse_attributes();
+}
 
-       fprintf(stderr, "Declarator type: ");
-       print_type(stderr, type);
-       fprintf(stderr, "\n");
+static void record_declaration(declaration_t *declaration)
+{
+       if(last_declaration != NULL) {
+               last_declaration->next = declaration;
+       } else {
+               if(context != NULL)
+                       context->declarations = declaration;
+       }
+       last_declaration = declaration;
+}
 
+static
+void maybe_push_declaration(declaration_t *declaration)
+{
        symbol_t *symbol = declaration->symbol;
 
        if(symbol != NULL) {
-               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;
-                       fprintf(stderr, "typedef '%s'\n", symbol->string);
-               } else {
-                       symbol->ID       = T_IDENTIFIER;
-               }
+               environment_push(declaration, context);
        }
 }
 
@@ -905,6 +977,8 @@ void parse_init_declarators(const declaration_specifiers_t *specifiers)
 
                parse_declarator(declaration, specifiers->storage_class,
                                 specifiers->type);
+               maybe_push_declaration(declaration);
+               record_declaration(declaration);
                if(token.type == '=') {
                        next_token();
                        if(token.type == '{') {
@@ -929,21 +1003,22 @@ 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 */
+                       /* TODO (bitfields) */
                } else {
-                       parse_declarator(&declaration, specifiers->storage_class,
+                       declaration_t *declaration
+                               = allocate_ast_zero(sizeof(declaration[0]));
+                       parse_declarator(declaration, specifiers->storage_class,
                                         specifiers->type);
+                       maybe_push_declaration(declaration);
+                       record_declaration(declaration);
 
                        if(token.type == ':') {
                                next_token();
                                parse_constant_expression();
-                               /* TODO */
+                               /* TODO (bitfields) */
                        }
                }
 
@@ -954,12 +1029,10 @@ void parse_struct_declarators(const declaration_specifiers_t *specifiers)
        expect_void(';');
 }
 
-static compound_entry_t *parse_compound_type_entries(void)
+static void 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));
@@ -969,9 +1042,10 @@ static compound_entry_t *parse_compound_type_entries(void)
 
                parse_struct_declarators(&specifiers);
        }
+       if(token.type == T_EOF) {
+               parse_error("unexpected error while parsing struct");
+       }
        next_token();
-
-       return entries;
 }
 
 void parse_declaration(void)
@@ -1303,7 +1377,7 @@ expression_t *parse_sub_expression(unsigned precedence)
 
        expression_parser_function_t *parser
                = &expression_parsers[token.type];
-       source_position_t             source_position = source_position;
+       source_position_t             source_position = token.source_position;
        expression_t                 *left;
 
        if(parser->parser != NULL) {
@@ -1444,7 +1518,7 @@ static
 statement_t *parse_label_statement(void)
 {
        eat(T_IDENTIFIER);
-       expect(';');
+       expect(':');
        parse_statement();
 
        return NULL;
@@ -1572,10 +1646,18 @@ statement_t *parse_declaration_statement(void)
        return NULL;
 }
 
+static
+statement_t *parse_expression_statement(void)
+{
+       parse_expression();
+       return NULL;
+}
+
 static
 statement_t *parse_statement(void)
 {
-       statement_t *statement = NULL;
+       declaration_t *declaration;
+       statement_t   *statement = NULL;
 
        /* declaration or statement */
        switch(token.type) {
@@ -1587,10 +1669,6 @@ statement_t *parse_statement(void)
                statement = parse_default_statement();
                break;
 
-       case T_IDENTIFIER:
-               statement = parse_label_statement();
-               break;
-
        case '{':
                statement = parse_compound_statement();
                break;
@@ -1635,6 +1713,22 @@ statement_t *parse_statement(void)
                statement = NULL;
                break;
 
+       case T_IDENTIFIER:
+               if(la(1)->type == ':') {
+                       statement = parse_label_statement();
+                       break;
+               }
+
+               declaration = token.v.symbol->declaration;
+               if(declaration != NULL &&
+                               declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
+                       statement = parse_declaration_statement();
+                       break;
+               }
+
+               statement = parse_expression_statement();
+               break;
+
        DECLARATION_START
                statement = parse_declaration_statement();
                break;
@@ -1648,12 +1742,20 @@ statement_t *parse_compound_statement(void)
 {
        eat('{');
 
-       int top = environment_top();
+       compound_statement_t *compound_statement
+               = allocate_ast_zero(sizeof(compound_statement[0]));
+       compound_statement->statement.type = STATEMENT_COMPOUND;
+
+       int        top          = environment_top();
+       context_t *last_context = context;
+       set_context(&compound_statement->context);
 
        while(token.type != '}') {
                parse_statement();
        }
 
+       assert(context == &compound_statement->context);
+       set_context(last_context);
        environment_pop_to(top);
 
        next_token();
@@ -1666,15 +1768,17 @@ translation_unit_t *parse_translation_unit(void)
 {
        translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
 
-       assert(translation_unit == NULL);
        assert(context == NULL);
-       translation_unit = unit;
+       set_context(&unit->context);
 
        while(token.type != T_EOF) {
                parse_declaration();
        }
 
-       translation_unit = NULL;
+       assert(context == &unit->context);
+       context          = NULL;
+       last_declaration = NULL;
+
        return unit;
 }
 
@@ -1683,7 +1787,10 @@ translation_unit_t *parse(void)
        obstack_init(&environment_obstack);
        environment_stack = NEW_ARR_F(environment_entry_t*, 0);
 
-       next_token();
+       lookahead_bufpos = 0;
+       for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
+               next_token();
+       }
        translation_unit_t *unit = parse_translation_unit();
 
        DEL_ARR_F(environment_stack);